I have a problem with a route with a dynamic GET parameter in Express 4.16 (and Node 12.1.0) : the route is never hit, but only for some values.

The route I'm trying to hit is this one :

app.get('/done/:taskid', function(req, res){

The route never matches for taskid = 0 or taskid = 1, and I just can't find out why. Works perfectly fine for taskid > 1. Actually, every value except 0 or 1 works (strings, null, whatever...).

The app doesn't crash, I'm not getting any log, but the code associated to the route is never executed and I'm just redirected to

app.get('/', function(req, res){

I'm very new in NodeJS, so it might be a basic error, but I just can't put my finger on it.

Here is the complete code of my main.js :

const app = require('express')();
const twig = require('twig');
const bodyparser = require('body-parser');

app.set('views', __dirname + "/views");
app.set('view engine', 'twig');
app.use(bodyparser.urlencoded());

var todolist = [];

app.get('/', function(req, res){
  res.setHeader('Content-Type', 'text/html');
  res.render('index.html.twig', {todolist: todolist});
});

app.get('/done/:taskid', function(req, res){
  console.log(req.params);
});

app.post('/new', function(req, res){
  //insert in DB later
  todolist.push({
    name: req.body.taskName,
    done: false
  });
  res.redirect(301, '/');
});

app.listen(8000);

I'm trying to access it via a simple link :

<a class="btn btn-sm btn-primary" href="/done/0">Mark as done</a>

Running the app via either pm2 or node doesn't change anything.

Does someone have some insight on how I could pass an id equal to either 0 or 1 in the link and successfully match this route ?

=================

Edit : So, there was apparently no bug... or at least not related to url matching. For some reason, the request was put in "stalled" state, and for a time long enough for me to think it didn't match anything (> 1min). Which is weird, because I run this app in a docker container, on my localhost. I have only 1 tab open, and I made sure to shut down and restart the app before testing several times, so I'm pretty sure there is only 1 client making request to my app... I went to sleep yesterday, came back this morning, exact same code started working. I have seriously no idea what happened.

=================

Edit 2 : Did a bit more testing, found a pattern. Problem seems relative to chrome. Code works perfectly fine with Firefox and Safari. Fails 100% of the time with Chrome ... BUT, it works 100% of the time with Chrome if I have the console panel opened... Like bug is sentient and happens only when nobody is watching...

1 Answers

0
Clément On Best Solutions

So, the problem was fixed by changing my <a href=""> into <form method="post" ...> and changing my routes accordingly. Still don't know what caused the problem, but it works now.

The links Andrew provided in the comments hint toward chrome prefetching my links and modifying my data ahead of the click. Doesn't explain why I didn't get any log thought.

Morals of the story : don't use GET methods to modify/delete data.