Here is a question I have concerning a Node.js
app of mine (working with Express
) on Heroku
.
I want to handle unknown URL. More precisely:
If I look at this URL in a browser: https://myapp.herokuapp.com/ I see what I expect. If I look a this one: https://myapp.herokuapp.com/xxyyyzzWhatever I also see what I expect. That is:
Cannot GET /xxyyyzzWhatever
Instead of displaying: Cannot GET /xxyyyzzWhatever I want to do other things. At this point I have referred to these documents: http://expressjs.com/en/guide/error-handling.html and http://thecodebarbarian.com/80-20-guide-to-express-error-handling; and I can get quite a bit of control on what is displayed.
I have implemented this kind of code:
app.get('*', function(req, res, next) {
setImmediate(() => { next(new Error('woops')); });
});
app.use(function(error, req, res, next) {
const xPt = req.path.substring(1,req.path.length);
res.json({ message: error.message reachPoint: xPt});
});
But what I really want is to have something like:
if (xPt.substring(0,1) == "A") {display("You are lucky");}
else if (xPt.substring(0,1) == "Z") {display("You are very unlucky");}
else if ((xPt.substring(0,1) >= "B")&&(xPt.substring(0,1) <= "R"))
{goto("https://stackoverflow.com/")}
else {goto("http://www.google.com/")}
Any tip on the way to go?