Open vishwaefor opened 5 years ago
Step 1 User should be able to register providing a name, username and password. Add an endpoint POST /users which can take user.json as body
POST /users
Step 2 Add body parser to the route
const bodyParser = require('body-parser'); router.use(bodyParser.json());
Step 3 Add bcrypt for hashing npm install bcrypt --save
npm install bcrypt --save
-Step 4
Save user in the db var express = require('express'); var router = express.Router(); const bodyParser = require('body-parser'); const bcrypt = require('bcrypt'); const Users = require('../models/users'); router.use(bodyParser.json()); router.post('/', function(req, res, next) { let user = req.body; new Promise((resolve, reject) => { bcrypt.hash(user.password, 10, (err, hash) => { if (err) { reject(err); } else { user.password = hash; resolve(user); } }); }) .then(user => { return Users.create(user); }) .then(user => { console.log('user created', user.name, user.email); res.statusCode = 200; res.setHeader('Content-Type', 'application/json'); res.json({ id: user._id, name: user.name, email: user.email }); }) .catch(err => { next(err); }); }); module.exports = router;
Steps
Step 1 User should be able to register providing a name, username and password. Add an endpoint
POST /users
which can take user.json as bodyStep 2 Add body parser to the route
const bodyParser = require('body-parser'); router.use(bodyParser.json());
Step 3 Add bcrypt for hashing
npm install bcrypt --save
-Step 4