next error handler was ignored in Express

Viewed 37

I am new to Express, and I am learning error handlers. My code is simple, it throws an error, write it to log and response.

const express = require('express');                                                   
const fs = require('fs');                                                             
const app = express();                                                                
                                                                                      
app.get('/', (req, resp, next) => {                                                   
        throw new Error('errors  !!!');                                                
});                                                                                   
                                                                                      
function writeLog(err, req, resp, next) {                                             
        fs.writeFile(                                                                 
                     './app.log',                                                     
                     `${req.method} ${req.url} Error: ${err.message}`,                
                     (err) => {                                                       
                         console.log('the callback');                                 
                         next(err);                                                   
                     }                                                                
        );                                                               
}                                                                                     
                                                                                      
function errResp(err, req, resp, next) {                                              
        console.log('next err handler');                                              
        resp.json(                                                                    
          {                                                                                 
            path: req.path,                                                               
            message: err.message,                                                         
          }                                                                                 
        );                                                                            
                                                                                      
}                                                                                     
                                                                                      
app.use(writeLog);                                                                    
app.use(errResp);                                                                     
                                                                                      
app.listen(8080, () => {                                                              
        console.log('listen on 8080');                                                
});   

The result is this: enter image description here

It writes error to app.log and calls the next function, but somehow the errResp handler was ignored, which confused me. The errResp should be called.

If I remove writeLog middleware, the errResp could be called and the server can response json structure correctly with 200 status code.

Node v12.4.0

Express v4.17.1

Anyone can help this beginner?

0 Answers
Related