Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding error message in Node.js&Express from POST access

I'm running a Restful API Server based on Node.js using Express.js.

Today, I realized that someone can get information of error of my source code, including directory path of my server, when he send request using Curl like curl -X POST ~.

Error message I get:

TypeError: Cannot read property 'text' of undefined at exports.list (/usr/local/node/XXX/routes/message.js:49:36) at callbacks (/usr/local/node/XXX/node_modules/express/lib/router/index.js:124:37) at param (/usr/local/node/XXX/node_modules/express/lib/router/index.js:118:11) at param (/usr/local/node/XXXv/node_modules/express/lib/router/index.js:125:11)

How can I hide those critical information when the server displays errors?

like image 1000
Jiwoong Kim Avatar asked Jul 13 '17 08:07

Jiwoong Kim


People also ask

How do I hide error messages in console?

console. clear(); is good option to hide the console error because some time on running site we don't want to show error so we hide them in PHP.

How can you find error in node js *?

An error in Node. js is any instance of the Error object. Common examples include built-in error classes, such as ReferenceError , RangeError , TypeError , URIError , EvalError , and SyntaxError .


2 Answers

I'd set the NODE_ENV flag to production, as the Express.JS doesn't send the stack data in this mode. I recommend you to check out the dotenv module in npm.

like image 103
Nikolay Schamberg Avatar answered Nov 11 '22 01:11

Nikolay Schamberg


You can create a middleware that deals with all unhandled errors for you.

app.use((err, req, res, next) => {
  if (err) {
    return res.sendStatus(500);
  }
  next();
});

That will return a 500 Internal Server Error status code to any user creating such an error. Remember to put this at the end of your middleware chain.

like image 21
Kris Selbekk Avatar answered Nov 11 '22 01:11

Kris Selbekk