Ellu
Ellu

Reputation: 1

Node.js Cannot find get route

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?

Upvotes: 0

Views: 43

Answers (1)

Bench Vue
Bench Vue

Reputation: 9390

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"));

Upvotes: 2

Related Questions