Express router route getting status 404 on Postman Code Answer

I’m trying to test routes done in express throught Postman but i keep getting status 404 despite i have another route that does work.

I won’t post the code to the routes that do work since there’s a lot of code written in here and seems unnecesary.

App.js

var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var cors = require("cors");
require("dotenv").config();
var bodyparser = require("body-parser")

var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var commentsRouter = require("./routes/comments")
var authRouter = require("./routes/auth")
var app = express();

var corsOption = {
  origin: true,
  methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  credentials: true,
  exposedHeaders: ['x-auth-token']
};

// view engine setup
app.use(cors(corsOption));
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: false }));

app.use(function (req, res, next) {
  if (req.originalUrl && req.originalUrl.split("/").pop() === "favicon.ico") {
    return res.sendStatus(204);
  }

  return next();
});

app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/comments", commentsRouter)
app.use("/auth", authRouter)

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});

module.exports = app;

Comments route

var express = require("express");
var router = express.Router();


//Get comment
router.get("/", function (req, res, next) {
    return res.status(200)
} )

/* Post new comment. */
router.post("/new", function (req, res, next) {
 return res.status(200)

});

//Update comments
router.put("/update", function (req, res, next) {
    
})

module.exports = router;

The index / route is working well but the /auth and /comments aren’t.

I’m trying with a GET to http://localhost:3000/comments

Answer

.status() wouldn’t end the response process. You need to explicit end it by chaining other methods such as .end() or .send().

Update the GET route of /comments with something like this.

router.get("/", function (req, res, next) {
    return res.status(200).end();
})

Related Posts

© No Copyrights, All Questions are retrived from public domain.
Tutorial Guruji