The express route parser is incorrectly detecting routes that it should not

I am encountering an issue with the following two routes:

router.get('/:postId([0-9]*)', handler) 
router.get('/:postId([0-9]*)/like', handler)

The first route is intended to only capture URLs like /posts/4352/, not /posts/3422/like. However, it seems to be matching both. I tested this using the official express route tester tool and it worked as expected there. (Try entering /posts/:postId([0-9]*) and /posts/2/like in the tester to see it doesn't match.)

What could be causing this incorrect matching in my case?

Note: I am aware that reordering the routes will fix the issue, but I am curious about why this behavior is occurring.

Answer №1

If you reverse the order, it will find a match based on the first hit unless next() is utilized, in which case it will not search for another match.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for incorporating extra details into a webpage without completely refreshing the content in Next.js when the URL shifts

Hello everyone, I'm diving into the world of Next.js and I could really use some assistance. I have a route called "workPage" which contains various navigation links. My goal is to display new information to users when one of these nav links is clicke ...

Dynamic Visualizations with D3.js: Live Charts for Real-Time

This is my first time using D3.js and I am attempting to create a basic sparkline graph. The graph should have time on the X-axis and numbers up to 1000 on the Y-axis. I have a web socket server that sends random numbers up to 1000 to clients. My goal is t ...

What techniques can I use to achieve a seamless transition using Javascript and CSS?

I'm striving for a seamless CSS transition. The concept of transition had me puzzled. Even though I utilized the setTimeout method in JavaScript to make my CSS functional, it lacks that SMOOTH feel! Check out my code below. function slideChange( ...

Sending a POST request in Node.js and Express may result in the request body being empty or undefined

Here is a snippet of my Typescript code: import express = require('express'); const app: express.Application = express(); const port: number = 3000; app.listen(port, () => { console.log("The server is now running on port" + port); ...

Troubleshooting issue with the spread operator and setState in React, Typescript, and Material-ui

I recently developed a custom Snackbar component in React with Material-ui and Typescript. While working on it, I encountered some confusion regarding the usage of spread operators and await functions. (Example available here: https://codesandbox.io/s/gift ...

Tips for forwarding Express arguments to asynchronous functions in Node.js

Currently, I am encountering an issue while working with express middleware. My main concern lies in checking for the existence of a file within my express middleware functions. //Organizing express middeleware... app.use(f1); app.use(f2); ... functi ...

Exploring the process of passing an array as a function argument from PHP to JavaScript

I am looking for assistance in passing an array as a function argument from PHP to JS. The values I need are retrieved from a database. while ($rows = pg_fetch_array($qry)) { ?> <option value="<?php echo $rows[&ap ...

Tips for integrating jwt token into axios request

I am facing an issue with my backend endpoint. I can successfully retrieve a list of customers using jwt token on Postman, but when I try to fetch the list from a React app using axios get request, it fails. After reading through this question, I implemen ...

Click on a link to open it in the current tab with customized headers

In my Angular project, I am attempting to open a link by clicking a button that redirects to the specified URL using the following code: window.open(MY_LINK, "_self"); However, in this scenario, I also need to include an access token in the header when t ...

Is there a way to extract data from the Redux state in a React component?

Seeking assistance with making an API request from Redux, followed by saving the data in my React state (arrdata). Although the API call is successful, I am facing challenges updating the App.js state based on the Redux API call. Any suggestions or insig ...

Which type of element does Youtube utilize for the videos on its own domain - <iframe> or <video>?

Do they have a different method for incorporating their videos? My goal is to utilize the playbackRate property on a non-embedded YouTube video for a Chrome extension. ...

When using $http post in Angular, I encountered an issue with an unhandled rejection error

After successfully setting up my $http post request in Angular, I encountered an issue when refreshing the page. The console displays the following error: Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"POST","transformRequest": ...

When HTML elements are dynamically inserted through JavaScript using quilljs, they may cause conflicts with the layout properties

I am currently working on creating a simple webpage layout similar to that of Stack Overflow, with a sidebar and a main content area that can scroll. In my case, the content area is intended to host a QuillJS text editor. To integrate the QuillJS editor i ...

The position of the scroll bar remains consistent as you navigate between different websites

Is it possible for me to click on a link within my HTML website and have the other website load with me in the exact same position? For example, if I'm halfway down a webpage and click a link, can I be taken to that same point on the new page? If so, ...

Constructing a query in Oriento using the "like" clause

I'm having trouble implementing autocomplete with an Oriento DB interface. The web server running on NodeJS with the Express framework has the following server code: express.get("/piatti", function(req, res) { var tipo = req.query.tipo; var ...

Access the JSON data stored in the File Directory by reading and utilizing it for your project

Can someone help me figure out how to retrieve data from the xmltojson.json file and assign it to a variable using JavaScript? const jsonfile = require('jsonfile') const file = 'xmltojson.json' jsonfile.readFile(file, function (err, obj ...

Preventing Content Changes When Ajax Request Fails: Tips for Error Checking

I was struggling to find the right words for my question -- My issue involves a basic ajax request triggered by a checkbox that sends data to a database. I want to prevent the checkbox from changing if the ajax request fails. Currently, when the request ...

Angular ng-if directive contains a specific class

I am trying to create a directive that will only be active when an element has a specific class. I have tried the following code: <feedback-analytics ng-if="$('#analyticTab').hasClass('active')"></feedback-analytics> Unfor ...

ExpressJS: Leveraging Dynamic Routing for Post and Put Requests

My tech stack includes Angular on the front end and Express on the back. I am facing a challenge with having two forms on a single page that need to post to the same URL, but trigger different controller methods based on a variable from the request body. ...

The beforeunload event only triggers when the page is refreshed, not when the page is exited

I am currently utilizing the react-admin framework (version 3.2) and I am attempting to trigger the beforeunload event when a user is moving away from the Edit form view. For instance, if my pathname is "feed/123", I want the beforeunload event t ...