Expressjs multiple optional parameters trigger request handler twice

Hello I need a second pair of eyes because I’m not sure why this is happening…
I want to create 1 request handler which might accept 0 or 1 or 2 parameters eg: http://hocalhost:3000/{seed}/{size}, seed and size parameters must be optional.

However the below example doesn’t work and the console.log is being printed twice. This doesn’t happen if I change the handlers route to /api/:seed?/:size?. Why is this happening & what am I doing wrong?

const sharp = require('sharp');
const express = require('express');
const settings = require('./settings');

const app = express();

const calculateDensity = (s) => {

    return (72 * s) / 180;

}

app.get('/:seed?/:size?', (req, res) => {
        
    console.log('Why am I seeing this log?');

    res.end();

})

app.listen(settings.PORT, () => {
    console.log(`Example app listening at http://localhost:${settings.PORT}`)
})

28 thoughts on “Expressjs multiple optional parameters trigger request handler twice”

  1. 440344 428269Does your blog have a contact page? Im having a tough time locating it but, Id like to send you an e-mail. Ive got some suggestions for your weblog you may be interested in hearing. Either way, excellent internet site and I look forward to seeing it expand over time. 559290

    Reply