I am using thunder client extension on vs code and I am requesting data from it. I tried changing names and my code a lot of times but it isnt working. Also i tried making name const but it is saying that name is undefined (void) so I think the problem is the req command is not getting my data from thunderclient. here's my index.js
const express = require('express');
const app = express();
const port = 4000;
const mongoDB = require("./db");
mongoDB();
app.get('/', (req, res) => {
res.send('Hello World!')
});
app.use('/api', require("./routes/CreateUser"));
app.use(express.json());
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});
Createuser.js
const express = require('express')
const router = express.Router()
const User = require('../models/User')
router.post("/createuser", async (req, res) => {
try {
await User.create({
name: req.body.name,
password: req.body.password,
email: req.body.email,
location: req.body.location,
})
res.json({ success: true })
} catch (error) {
console.log(error)
res.json({ success: false })
}
})
module.exports = router
Thunderclient Thunderclient
So I guess I have some problems in my thunderclient as it is not sending the json data because name is returning void.
The order of express middleware matters. You need to put the
app.use(express.json())statement before using your API router.