I followed all the docs recomendation and still not working
const express = require('express')
const app = express()
const errorHandler = require('./middleware/errorHandler')
app.use(express.json())
app.use(errorHandler)
app.get('/home', (req,res) => {
console.log('home')
res.send("home")
})
THE MIDDLEWAER FUNCTION IS IN THE MIDDLEWARE FOLDER
const errorHandler = ( err, req, res, next) => {
console.log("error mddleware is working")
next()
}
CodePudding user response:
Add module.exports = errorHandler
under your middleware function.
CodePudding user response:
Make sure the error is used correctly. Always put the handler last, before the port listener :
const express = require("express")
const app = express()
const errorHandler = require("./middleware/errorHandler")
app.use(express.json())
app.get("/home", (req, res) => {
console.log("home")
throw new Error("error")
res.send("home")
})
app.use(errorHandler)
app.listen(3000, () => console.log("server running on port 3000"))