Node, Mongoose, Rest Api
20 October 2017
npm install
mongod
npm start
use Postman to test routes
the app is served on port 3000
For testing create db testDB
npm test
/*---------------------------------------------------------------------
POST user - creates a new user
check to see if that user exists by checking email in form against database
use schema's `create` method to insert document into Mongo
if user doesnt add a full name or email we cant create a user
----------------------------------------------------------------------*/
router.post('/', function(req, res, next) {
User.findOne({emailAddress:req.body.emailAddress})
.exec(function(err, user){
if(user){
err = new Error();
err.message = 'That email already exists';
err.status = 400;
return next(err);
}else{
User.create(req.body, function (err, user) {
if(!user.emailAddress || !user.fullName || !user.password){
err.status = 400;
return next(err);
}
if (err){
return next(err);
} else{
res.location('/');
res.status(201).json();
}
});
}
});
});
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.