I'm somewhat new to nodejs.
Using express / npm.
Once i hit '/' from my browser, I get this in output:
[TypeError: Object #<Object> has no method 'push']
No line numbers, no traceback, nothing.
I need to be able to fix this error, and to do that I need to know what file / line # this occurred.
How do I get nodejs to output a traceback?
First, add an express error handler, which is middleware expecting 4 arguments:
app.use(function (error, req, res, next) {
console.error(error.stack);
});
Make sure to put the above code at the very end of your middleware stack after all your normal middleware and route handlers are defined.
If that doesn't get you your stack trace, catch process-level uncaught exceptions:
process.on("uncaughtException", function (error) {
console.error(error.stack);
});
And one final tip would be run your app under the debugger with node --debug=3001 app.js, connect to it with node-inspector, open the debugger in chrome, enable "stop on exceptions", then trigger the error. The debugger will take you to the line where the exception occurs.
Related
How to handle the error: Error: getaddrinfo ENOTFOUND unexistantwebsite.com with the node package Websocket(Ws) without making the app crashing ?
I just tryed to create a websocket connect on an non-existant website and that make all the app crashing :
var ws = new WebSocket('wss://unexistantwebsite.com/', {
binaryType: "arraybuffer"
});
I tried many way to handle the error but nothing works.
Surround with try catch: KO
Add .catch on the constructor end: KO (even invalid that's normal I suppose)
I just want to handle the error avoiding the app to crash.
I have a challenge using the Node's process.on function in the Vue CLI project's main.js file.
I have the following code in the file.
process.on("unhandledRejection", function(reason, promise){
console.log("Unhandled", reason, promise); // log all your errors, "unsuppressing" them.
throw reason; // optional, in case you want to treat these as errors
});
The project builds but when the app is opened on the browser, I get an error:
Uncaught TypeError: process.on is not a function
How do I configure the project so that I can use the Node's process?
Am I not understanding NodeJS in this context?
Is there something I am missing?
Any assistance is appreciated thanks.
I'm using pm2 to manage process in my nodejs express application (running in cluster mode).
We had 2 kind of error handler
FIRST: 'uncaughtException' will be handled with
process.on('uncaughtException', function(err){});
Actually, I do not declare an handler like this cause of letting pm2 detect died worker in this case so restart the died worker automatically.
SECOND: express error handler, I mean the error will be forwarded to express error handler, not uncaughtException handler, the error handler like below
app.use(function(err, req, res, next) {})
I also do not declare this error handler for same purpose as uncaughtException.
But pm2 does not restart node in this case.
Any idea about this problem?
Many thanks
When catching errors with express error handler or even "uncaughtException" event, the process is still running, so pm2 won`t restart it.
If you want pm2 to restart after each exception, I would suggest something like this:
process.on('uncaughtException', function(e) {
console.log('An error has occured. error is: %s and stack trace is: %s', e, e.stack);
console.log("Process will restart now.");
process.exit(1);
})
Same goes for the express error handler. When we execute process.exit method, the process will terminate and pm2 will restart it.
First of all, I tried to follow this question (but adapted to Express 3.0):
Nodejs: How to catch an exception from middleware?
app.use(function(err, req, res, next) {
console.error('Middleware exception:' + err.stack);
next(err);
});
However, no matter where I put it in my middleware chain, before or after app.use(lessMiddleware); (from require('less-middleware')) I still get:
GET /? 304 44ms
source : <my path>/style.less
dest : <my path>/style.css
read : <my path>/style.less
DEBUG:
DEBUG: <my path>/node_modules/less-middleware/node_modules/less/lib/less/parser.js:385
throw new(LessError)(e, env);
^
DEBUG: [object Object]
DEBUG: Program node app.js exited with code 1
Kind readers, how to solve this problem? I want to:
Show errror stacktrace
Show server error page instead of crashing
To my surprise the problem was in less-middleware. I kind of expected it to be a more mature solution. I fixed it and made a pull request on Github. Hope this helps anybody encountering the same problem. Cheers.
I have a simple Express server that is serving up some static files. Here is the server:
var express = require('express');
var app = express.createServer();
// Configuration
app.configure(function() {
app.use(express.bodyParser());
app.use(express.staticCache());
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
// 404
app.get('*', function(req, res) {
res.send('not found', 404);
});
app.listen(3000);
In my public directory I have a file called index.html. Firing up node app.js and then browsing to localhost:3000/index.html shows the static file as expected. Navigating to localhost:3000/ind or localhost:3000/ind\ shows the 404 page as expected.
However, navigating to localhost:3000/index.html\ (note the trailing backslash) crashes my node server with:
stream.js:105
throw er; // Unhandled stream error in pipe.
^
Error: ENOENT, no such file or directory '/home/bill/projects/app/public/index.html\'
Why is the node server crashing instead of just serving up the 404 page? I thought since the file does not exist, the static middleware would just skip it and pass the request on to the routes. I got around it by creating a custom middleware that returns 404 if a trailing backslash exists in the request URL, but I'd like to figure out if I'm missing something here. Thanks!
The reason for this behavior seems to be the difference in how fs.stat and fs.createReadStream handle trailing backslashes.
When the string 'path/to/public/index.html\\' is given to fs.stat in the static middleware, it is ignored (running stat index.html\ on the command line checks for a file named index.html, you'd have to run stat index.html\\ for index.html\). So fs.stat thinks the file was found because it thinks you're asking for index.html, and doesn't call the next middleware handler.
Later, that string is passed to fs.createReadStream which thinks it's looking for index.html\. It doesn't find that file and throws said error.
Since the functions treat the backslash differently, you can't really do anything but use some middleware to filter out those requests.