By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to prevent “*” route from running with the routes defined previously
JavaScript

Javascript – How to prevent “*” route from running with the routes defined previously

Admin
Last updated: 2024/02/10 at 3:31 PM
Admin
Share
2 Min Read
How to prevent "*" route from running with the routes defined previously

Problem:

I defined my app routes in that order

Contents
Problem:Solution:
app.use(express.json());
app.use(express.static(`${__dirname}/public`));
app.use('/api/v1/tours', tourRouter);
app.use('/api/v1/users', userRouter);

app.all('*', (req, res, next) => {
  const error = new AppError(
    `This ${req.originalUrl} is not on the server`,
    404,
  );
  next(error);
});

app.use(globalErrorHandler);

The AppError is a class to customize the error object and the globalErrorHandler is function that send the error response according to the error object comes from next().

My problem is that the ‘*’ routes runs with all the routes that I handled previous with the tourRouter. I don’t know why this happens. It wasn’t doing this, just happens once and still happening until the time I wrote this problem.

NOTE: Postman gives me the correct response from the routes I defined, but the terminal in VSC gives me the “cannot send header after it sent” error.

I don’t know what to do since I don’t know why the error happened.

Solution:

I’ve discovered the error which was that I rapped my async router handler functions with a function called catchAsync()

module.exports = function (fn) {
  return (req, res, next) => {
    fn(req, res, next).then(next);
  }
}

I’ve updated this function to be this

module.exports = function (fn) {
  return (req, res, next) => {
    try {
      fn(req, res, next);
    } catch (error) {
      next(error);
    }
  };
};

I made this because the catchAsync function calls the next after the fn() ends which causes to run the next middleware in the stack which is globalErrorHandler

app.use(globalErrorHandler);

to run and send another response and from here the error happened
But what makes me still confused is that the code works fine and then starts throwing errors

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How can I change rowsPerPage in MaterialReactTable? Javascript – How can I change rowsPerPage in MaterialReactTable?
Next Article Connecting CSS and JSS to HTML on the WSGI client Javascript – Connecting CSS and JSS to HTML on the WSGI client
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?