Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Express ignores response.send() call

I have a simple site:

const express = require("express");
 
const app = express();
 
app.set("view engine", "hbs");
 
app.get("/about-me", function(_, response) {
    response.render("about_me.hbs");
});
app.get("/", function (_, response) {
    response.render("index.hbs");
});
app.all(/.*/, function (_, response) { // 404 page
    response.sendStatus(404);
    response.send("<h1>404 Not Found</h1>");
}
app.listen(3000);

But when I opened http://localhost:3000/not-exist , there was "Not Foun<" text. Why it’s happening and how to fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t send two responses to a single request.

Use sendStatus or send.

If you want to control the status code when you use send, use status.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading