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

How can I store or obtain the inaccurate route that was searched for?

In this case, how could I save the /india route in a variable in the router? app.get(‘*’, function(req, res){ //Save rute here; }

>Solution :

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

If you just want to log the bad route to a file, you can do something like this. Just make this is the last route you define so that all other routes get a chance to match before it does:

app.get("*", (req, res) => {
    // log the unhandled route
    fs.appendFile("bad-routes.txt", req.path + "\n", (err) => {
        if (err) {
            console.log(`Error attempting to append ${req.path} to bad-routes.txt`);
        }
    });
    res.sendStatus(404);
});

Note, in this list, you will probably find things that crawlers (not users) are also probing your site for.

The above logs only GET requests. If you want to log for all http verbs, you could do this:

app.use((req, res) => {
    // log the unhandled route
    fs.appendFile("bad-routes.txt", `${req.method}: ${req.path}\n`, (err) => {
        if (err) {
            console.log(`Error attempting to append ${req.path} to bad-routes.txt`);
        }
    });
    res.sendStatus(404);
});
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