Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
express-ws is silencing errors

I am use express and express-ws in a node application. However, express-ws seems to be silencing errors making it very difficult to debug my code.

I've reduced my code down to just the following which still reproduces the problem:

// configure express
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

// set up routes

app.get('/', function(req, res) {
    console.log("GET");
    nonExistingFunction(); // Called to demonstrate a traceback is provided.
});

app.ws('/', function(ws, req){    
    console.log("WS");
    nonExistingFunction(); // Called to demonstrate this error is silenced.
});

// start service
app.listen(90);

When I GET from a browser, the console shows the 'GET' log message followed by the traceback (as expected). I.e;

GET
ReferenceError: nonExistingFunction is not defined
    at C:\(project dir)\server.js:11:9
    at Layer.handle [as handle_request] (C:\(project dir)\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\(project dir)\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\(project dir)\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\(project dir)\node_modules\express\lib\router\layer.js:95:5)
    at C:\(project dir)\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\(project dir)\node_modules\express\lib\router\index.js:335:12)
    at next (C:\(project dir)\node_modules\express\lib\router\index.js:275:10)
    at expressInit (C:\(project dir)\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (C:\(project dir)\node_modules\express\lib\router\layer.js:95:5)

When I connect via Websocket, the console just shows the 'WS' log message - no traceback. I.e;

WS

Does express-ws silence errors by default? Is there a way to switch it off?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I have a solution.

Express has its own default error handler which outputs the traceback to the console but allows the application to continue running. Express-ws uses a try-catch nest to do the same in wrap-middleware.js.

  try {
    /* Unpack the `.ws` property and call the actual handler. */
    middleware(req.ws, req, next);
  } catch (err) {
    /* If an error is thrown, let's send that on to any error handling */
    next(err);
  }

However, this doesn't seem to get passed down to the default error handler. So instead, you can specify a error handler middleware for app. This must be the last middleware in the chain. The following code is a little hacky, but will output tracebacks to the console regardless of whether the error occurred within a HTTP or a WS route function.

// configure express
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

// set up routes

app.get('/', function(req, res) {
    console.log("GET");
    nonExistingFunction(); // Called to demonstrate a traceback is provided.
});

app.ws('/', function(ws, req){    
    console.log("WS");
    nonExistingFunction(); // Called to demonstrate this error is no longer silenced :)
});

// set up error handler

function errorHandler (err, req, res, next) {
    if(req.ws){
        console.error("ERROR from WS route - ", err);
    } else {
        console.error(err);
        res.setHeader('Content-Type', 'text/plain');
        res.status(500).send(err.stack);
    }
}
app.use(errorHandler);

// start service
app.listen(90);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!