Skip to content

Instantly share code, notes, and snippets.

@thebergamo
Last active August 29, 2015 14:03
Show Gist options
  • Save thebergamo/c1c8e8e724f7e1421181 to your computer and use it in GitHub Desktop.
Save thebergamo/c1c8e8e724f7e1421181 to your computer and use it in GitHub Desktop.
express.js
var express = require('express');
var domain = require('domain');
var app = express();
app.use(function(req, res, next){
var d = domain.create();
d.add(req);
d.add(req);
d.on('error', next);
d.run(next);
});
app.use(function(req, res, next){
next(new Error('Página não encontrada'));
});
app.use(function(err, req, res, next){
console.log(err);
console.log(err.stack);
res.json(500, {error: err});
});
module.exports = app;
SyntaxError: Unexpected token }
at Object.parse (native)
at parse (/srv/nodeJS/taska/node_modules/body-parser/lib/types/json.js:64:17)
at /srv/nodeJS/taska/node_modules/body-parser/lib/read.js:96:18
at IncomingMessage.onEnd (/srv/nodeJS/taska/node_modules/body-parser/node_modules/raw-body/index.js:135:7)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:929:16
at process._tickCallback (node.js:419:13)
{
"email": "marcos@thedon.com.br",
"name":{
"last": "Lord",
}
}
app.post('/api/user', bodyParser.json(), function(req, res){
try{
var json = JSON.parse(req.body)
}catch(err){
res.json(400, {error: err});
}
if(req.body.email === undefined){
res.json(400, {error: 'Email is required'});
return;
}
if(req.body.name === undefined){
res.json(400, {error: 'Name is required'});
return;
}
if(req.body.name.first === undefined){
res.json(400, {error: 'First Name is required'});
}
if(req.body.name.last === undefined){
res.json(400, {error: 'Last Name is required'});
}
console.log(req.body.email);
console.log(req.body.name.last);
var user = new model({
email: req.body.email,
name: {
first: req.body.name.first,
last: req.body.name.last
}
});
user.save(function(err, user){
if(err)
res.json(err);
res.send(202, user);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment