in Education by
What I'm trying to do here is use the 2 functions but I don't know how to use both of them as they're both post functions with same name and I want to able to test using postman. These are in the same file const jwt = require('jsonwebtoken'); const Joi = require('joi'); const bcrypt = require('bcrypt'); const _ = require('lodash'); const { users } = require('../models/user'); const express = require('express'); const router = express.Router(); this is the first post function router.post('/', async (req, res) => { const { error } = validate(req.body); if (error) { return res.status(400).send(error.details[0].message); } let user = await users.findOne({ email: req.body.email }); if (!user) { return res.status(400).send('Incorrect email or password.'); } const validPassword = await bcrypt.compare(req.body.password, user.password); if (!validPassword) { return res.status(400).send('Incorrect email or password.'); } const token = jwt.sign({ _id: user._id }, 'PrivateKey'); res.header('x-auth-token', token).send(_.pick(user, ['_id', 'name', 'email'])); }); this is the second post function router.post('/', async (req, res) => { const { error } = validate(req.body); if (error) { return res.status(400).send(error.details[0].message); } let user = await users.findOne({ email: req.body.email }); if (!user) { return res.status(400).send('Incorrect email or password.'); } const validPassword = await bcrypt.compare(req.body.password, user.password); if (!validPassword) { return res.status(400).send('Incorrect email or password.'); } }); function validate(req) { const schema = { email: Joi.string().min(5).max(255).required().email(), password: Joi.string().min(5).max(255).required() }; return Joi.validate(req, schema); } and lastly the last line in the file module.exports = router; JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You cannot have 2 handlers for same REST endpoint with same METHOD. Why don't you change one to router.post('/login')

Related questions

0 votes
    What I'm trying to do here is use the 2 functions but I don't know how to use both of them ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm getting unexpected results for getting the collections between two columns. I have this simple collection: ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'm getting unexpected results for getting the collections between two columns. I have this simple collection: ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    so i'm reviewing and practicing making rest api with node mongoose and express. I'm having problem making ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    The napi_create_date function appeared in Node.js starting with 11.11.0 version. https://nodejs.org/api/n- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm trying to debug my nodejs app using node-inspector. But Google Chrome doesn't show the code. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I have been looking at using the ssh2 module for sftp shipping of logs. However the cloud service hosting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    Quick Summary of my question: Does Gulp 3.6.0 dest() handle glob-base the same way as 4.0.0? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I'm trying to make a form-maker with Node.Js and MongoDB but confused about the data structure. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Here is my data: { { "_id" : ObjectId("623a92139e19f99295167786"), "headline" : "headline1", " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    app.route('/users') .post(user.post) .get(user.get) .get(user.everyone) .put(user.update) . ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I'm used to v3 node botbuilder sdk so I have a middleware where I look at the dialog stack and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to use node to print the Serial from Arduino uno connected to USB I have a file script ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I've been putzing with trying to call the jobs.Insert bigquery rest api endpoint with node (the jobs. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
...