i am building a backed end for my app, but when i am trying to post a post req from postman to my server for testing that is i am going right, but unfortunately my server don't even get any req from postman, inside my postman software is showing Sending request... for 2-3 mins than it's showing me Could not get response,
Does anyone know How can i fix it? i tried to read my code 3-4 times but i can't find my mistake, i also checked spelling, file locations, and even restarted my server but can't find my mistake yet, can anyone tell me where i am wrong?
server.js:
const express = require('express')
const port = 3000
const app = express();
const bodyParser = require('body-parser');
require('./db');
require('./Models/user');
const Authroutes = require('./routes/Auth');
app.use(bodyParser.json);
app.use(Authroutes);
app.get('/', (req, res) => {
res.send('Hello World')
})
app.listen(port, () => {
console.log('sever is running at 3000!');
})
Auth.js:
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const User = mongoose.model('User');
const jwt = require("jsonwebtoken");
require('dotenv').config();
const nodemailer = require("nodemailer");
router.post('/verify', (req, res) => {
console.log(req.body)
})
module.exports = router;
[![enter image description here][1]][1]
CodePudding user response:
You have not configured the bodyParser middleware appropriately.
Change this:
app.use(bodyParser.json)
to this:
app.use(bodyParser.json());
You need the parens after bodyParser.json()
to generate the proper middleware function. Passing bodyParser.json
by itself wasn't generating an error, but it also wasn't working properly either.
In fact, you can simplify further by removing the body-parser module entirely because that is built into Express so, you can remove this:
const bodyParser = require('body-parser');
And, change this:
app.use(bodyParser.json)
to this:
app.use(express.json());