Last active
August 29, 2015 14:03
-
-
Save thebergamo/c1c8e8e724f7e1421181 to your computer and use it in GitHub Desktop.
express.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
"last": "Lord", | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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