'Express Server how to post data from postman
I just built a express server with mongodb and trying to request it using postman but getting error So this is the code for the route
router.post(
'/',
check('email', 'Please include a valid email').isEmail(),
check('password', 'Password is required').exists(),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const {email,password}=req.body;
var FirstTimer= new User({
email:email,
password:password
});
FirstTimer.save(function(err,result){
if (err){
console.log(err);
}
else{
console.log(result)
}
})
}
)
code for server.js
app.get('/', (req, res) => {
res.send('Hello World!')
})
connectDB();/*connect to mongoose which in turn connect to db*/
app.use('/api/auth',require('./routes/api/auth'));
app.listen(port,()=>console.log('listening on 3000'))//mention port here
Solution 1:[1]
I was using JSON data, so I had to use app.use(json()) in the server.js
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |



