Avoiding overlapping GET requests in express.js

Aniket Prakash
1 min readMar 27, 2021

I faced with such issue when I was working on a project with large codebase. After keen observation I found the problem that a route with url params was specified before the main route due to which it was not accessible.

const express = require('express');const app = express();app.get('/:home', (req,res)=>{res.send('params home');})app.get('/home', (req,res)=>{res.send('home');})app.listen(3000);

In the above example the problem can be observed. The original home route can’t be accessed beacause of the parameter route. This occured beacause the program runs sequentially from top to bottom and this should be kept in mind. Therefore, the parameter routes should always be specified below the actual route paths.

Like this :

const express = require('express');const app = express();app.get('/home', (req,res)=>{res.send('home');})app.get('/:home', (req,res)=>{res.send('params home');})app.listen(3000);

This issue may seem very small but it can cause issue in bigger projects. Follow the best practices while making any project which will avoid any complications afterwards and will make it easy to find and fix bugs in the code.

--

--