0
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const Observation = require("./schema/dreamWorld");
const bodyParser = require("body-parser");
const cors = require("cors");

app.get("/", (req, res) => res.send("Hello World"));
app.listen(3000, () => console.log("Server started on port 3000"));
app.use(cors, bodyParser.json());

app.get("/world", (req, res) => {
  return res.status(200).json({ message: "HAppy?" });
});
const mongoDB = "mongodb://127.0.0.1/test";
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });

in the above coding that I am testing in Postman! I can connect to "/" route and postman displays hello world! but when I search "/world" get route in postman it keeps on loading forever and does not fetch anything.

I have made sure server is running with correct Port number. what am I doing wrong here?

Ellu
  • 1
  • 1

1 Answers1

2

The body-parser no more necessary if use express V4.16 or later. It is included into express.js.

More detail is explained in here.

This code will be works.

const express = require("express");
const mongoose = require("mongoose");
const Observation = require("./schema/dreamWorld");
const cors = require("cors");

const app = express();
app.use(cors())

app.get("/", (req, res) => res.send("Hello World"));

app.get("/world", (req, res) => {
  return res.status(200).json({ message: "Happy?" });
});

const mongoDB = "mongodb://127.0.0.1/test";
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });

app.listen(3000, () => console.log("Server started on port 3000"));
Bench Vue
  • 5,257
  • 2
  • 10
  • 14