Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
How can I make Express router work this way?

I'm doing a project for school, I need to create a Rest API for a blog with Node.js. Users can post articles, and post comments related to these articles. For this project I'm using Sequelize and Express.

In my "comments" table, there is a "article" column which contains the title of the article where the comments has been posted. So I'm using a url like the following to get article's id, and then the title : /articles/:id/comments

The way I wanted the router's structure to be is the following :

  • to post a comment -> POST localhost/articles/:id/comments
  • to list all comments of a specific article -> GET localhost/articles/:id/comments
  • to list all comments of the whole blog -> GET localhost/articles/comments

The problem is that when I make a GET localhost/articles/comments request, the router trigger this part of the code that leads to a Sequelize BAD_FIELD_ERROR :

file: routes/article.js

router.get("/:id", (req, res) => {
    const id = parseInt(req.params.id);
    Article.findByPk(id).then((article) => {
        if (!article) res.sendStatus(404);
        else res.json(article);
    });
});

Instead of this part :

file: routes/article.js

router.get("/comments", (req, res) => {
    const Query = req.query;
    Comment.findAll({
        where: Query
    }).then((comments) => res.json(comments))
});

This is the server.js file :

const express = require("express");
const securityRouter = require("./routes/security");
const userRouter = require("./routes/user");
const articleRouter = require("./routes/article");
const verifyWebToken = require("./middleware/verifyWebToken");
const app = express();

const connection = require("./lib/db");
connection.sync();

app.use(express.json());

app.use("", securityRouter);
app.use("/users", userRouter);
app.use("/articles", verifyWebToken, articleRouter);

app.listen(3000, () => console.log("Server is listening."));
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!