Home > Software engineering >  Express get request with an :id-parameter being run twice
Express get request with an :id-parameter being run twice

Time:11-24

I have an API made with node and express, and the end goal is to store the :id-parameter from the request in a variable so that I can query a specific table from my SQLite database with a specific id. Now the get request with an :id-parameter is being run twice, once with the request url being what it´s supposed to be (in my case "page") and once with the url /serviceWorker.js.

This is the code from my router with the get-request:

import express from "express";
const router = express.Router();

router.get("/:id", function (req, res) {
  const id = req.params.id;
  console.log("id:", id);
  console.log("Req url:", req.url);
  res.send(id);
});

export default router

The console log from a single get request looks like this:

id: molekylverkstan
Req url: /molekylverkstan
id: serviceWorker.js
Req url: /serviceWorker.js

Even when running the get request for my home page, with this code:

router.get("/", function (req, res) {
  res.render("../pages/start.ejs");
});

I get the console log:

id: serviceWorker.js
Req url: /serviceWorker.js

I tried breaking the request with break(), return, and next() but nothing worked. I have no service worker in my project which makes me wonder why it comes out of nowhere. Maybe I need som kind of service worker, or to specifically state somewhere in my project that it shouldn´t look for one?

CodePudding user response:

Even though you don't have a service worker in your project, there might still be one registered in your browser from a previous project that you ran on the same domain/IP address in the past.

The reason you are getting this request is that your browser no longer has the valid cached version of the service worker and tries to reload it. But since that request fails, it will try over and over again.

To remove it, open the website in question and check your browser developer tools (Application -> Service Workers or chrome and firefox), and unregister any service workers you no longer want to use.

  • Related