Skip to content

Instantly share code, notes, and snippets.

@macedigital
Created April 26, 2018 11:11
Show Gist options
  • Save macedigital/090fe2b244728b089ea60fa25154a352 to your computer and use it in GitHub Desktop.
Save macedigital/090fe2b244728b089ea60fa25154a352 to your computer and use it in GitHub Desktop.
Handle runtime exception with express-js
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const xmlparser = require('express-xml-bodyparser');
app.use(xmlparser());
app.post('/xml', (req, res, next) => {
console.log(req.body); // handle parsed xml
res.setHeader('content-type', 'text/plain');
return res.send('yay, xml received!');
});
app.use((err, req, res, next) => {
console.error(err); // handle error or just log it
return res.send('damn, something went wrong!');
})
server.listen(1337);
@macedigital
Copy link
Author

Example of using ExpressJS error-handling for catching malformed xml; see macedigital/express-xml-bodyparser#21.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment