Last active
July 23, 2017 14:51
-
-
Save adnanrahic/1c6433b8d7bbc8d86b3fc61a67c740a6 to your computer and use it in GitHub Desktop.
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
// Service | |
module.exports = { | |
createUser: createUser | |
}; | |
function createUser(req, res, next) { | |
return User.findOne({neki id}) | |
.then(user => { | |
if (user.exists) return res.status(500).send('zajebo si'); | |
}) | |
.then(() => { | |
return User.create(req.body); | |
}) | |
.then((user) => { | |
return res.status(200).send(user); | |
}) | |
.catch(err => next(new Error(err))); | |
} | |
// validate user middleware | |
function validateUser(req, res, next) { | |
if (typeof req.body.email === 'string' && | |
typeof req.body.fullName === 'string' && | |
typeof req.body.username === 'string') | |
return next(); | |
return res.status(500).send('Please enter a valid User.'); | |
} | |
module.exports = validateUser; | |
// Controller | |
var express = require('express'); | |
var UserController = express.Router(); | |
var UserService = require('path/to/UserService'); | |
var validateUser = require('path/to/validateUser'); | |
UserController.post('/api/users', validateUser, UserService.createUser); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment