I have been trying to develop an API for use in a MEAN stack application. The API handles user registration and authentication. What's been really funny is that it has not been taking any requests from Postman. Even a simple '/' get request with a response will not work. The error I get on postman is as below
My index.js is as below:
const exp = require('express');
const bp = require('body-parser');
const { success, error } = require('consola')
const { connect } = require('mongoose');
// Bring in the app constants
const { DB, PORT } = require("./config");
// Initialize the application
const app = exp();
// Call the middleware
app.use(cors());
app.use(bp.json);
// User Router Middleware
app.use("/api/users", require("./routes/users"));
app.use("/basic", require("./routes/basic"));
// Connect to the database
const startApp = async () => {
try {
// Connection With DB
await connect(DB, {
useFindAndModify: true,
useUnifiedTopology: true,
useNewUrlParser: true
});
success({
message: `Successfully connected with the Database \n${DB}`,
badge: true
});
// Start Listenting for the server on PORT
app.listen(PORT, () =>
success({ message: `Server started on PORT ${PORT}`, badge: true })
);
} catch (err) {
error({
message: `Unable to connect with Database \n${err}`,
badge: true
});
startApp();
}
};
startApp();
My basic routing is as follows, it itself will not even work for me:
router.get('/', function (req, res) {
res.send('Hello World!' + req.body)
})
module.exports = router;
The postman response is as in the screenshot below:
The server does appear to be running but is just refusing all requests it seems.
mongodb://localhost:27017/node-auth
SUCCESS Server started on PORT 3000 16:26:36
My apologies.
I was missing a () brace which was allowing the application to run but not correctly. Sometimes all it takes is another eye.
Before:
app.use(bp.json);
After: app.use(bp.json());
Working perfectly now.