'Express API work locally but not on Nginx server

I've developed an API with Express that work locally but not on server with Nginx, I get a 404 error for each others pages than "/" and I can't figure why. I saw lot of persons recommend to use app.get instead of app.use but that imply not using router and my API is quite big so It won't be good for me.

That's my server.js file :

const express = require("express");

const app = express();
const port = 3000;

app.use(express.json({ limit: "100mb" }));

const authRouter = require("./routes/auth");
const jobsRouter = require("./routes/jobs");

app.use("/auth", authRouter);
app.use("/jobs", jobsRouter);

app.listen(port, () => {
  console.log(`Listening on port ${port}...`);
});

and here it's the jobs file for example :

const express = require("express");
var fs = require("fs");

const auth = require("../middleware/auth");
const { admin, editor, viewer } = require("../middleware/roles");

let selectedData = require("../data/jobs.json");

const router = express.Router();

router.get("/", [auth, viewer], (req, res) => {
  res.send({
    selectedData,
  });
});

router.post("/", [auth, editor], async (req, res) => {
  let toPut = {
    id: selectedData.length + 1,
    data1: req.body.data1,
    data2: req.body.data2,
    data3: req.body.data3,
    data4: req.body.data4,
  };
  selectedData.push(toPut);

  fs.writeFile(
    "./data/jobs.json",
    JSON.stringify(selectedData),
    function writeJSON(err) {
      if (err) return console.log(err);
    }
  );

  res.status(200).send({
    selectedData,
  });
});

router.delete("/:id", [auth, admin], async (req, res) => {
  delete selectedData[req.params.id - 1];

  res.status(200).send({
    selectedData,
  });
});

module.exports = router;

and there my Nginx config :

server {

        root /var/www/webiste.com;
        index index.html index.htm index.js index.nginx-debian.html;

        server_name webiste.com www.webiste.com;

        location / {
                proxy_pass http://localhost:3000;
                }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/webiste.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/webiste.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = www.webiste.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = webiste.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name webiste.com www.webiste.com;
    return 404; # managed by Certbot

The fact is that if I put app.get("/api/jobs", something...) in server.js it works but not with app.use so if you have any ideas of why the router isn't working It will be great !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source