I've been working on creating an API for a MEAN stack application that handles user registration and authentication. However, I've run into a strange issue where the API is not responding to any requests made through Postman, not even a simple '/' get request with a response.
Below is my index.js file:
const exp = require('express');
const bp = require('body-parser');
const { success, error } = require('consola')
const { connect } = require('mongoose');
// Importing app constants
const { DB, PORT } = require("./config");
// Initialize the application
const app = exp();
// Set up 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 {
// Connect to DB
await connect(DB, {
useFindAndModify: true,
useUnifiedTopology: true,
useNewUrlParser: true
});
success({
message: `Successfully connected with the Database \n${DB}`,
badge: true
});
// Start 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, but it's also not working for me:
router.get('/', function (req, res) {
res.send('Hello World!' + req.body)
})
module.exports = router;
The Postman response shows an error, which you can see in the screenshot below:
https://i.sstatic.net/TXU40.png
Although the server seems to be running, it is rejecting all requests.
mongodb://localhost:27017/node-auth
SUCCESS Server started on PORT 3000 16:26:36