Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

global error handling middleware express

//app.js
//we need to put this code at last,this code run when there is no route match
app.all('*',(req,res,next)=> {
     const err= new Error(`Can't find ${req.originalUrl} on this server!`)
     err.status=404
     err.statusCode=404
    next(err)
})
//when we use next(err) it will go to error handling middleware and it will catch error and send response.
app.use((err,req,res,next)=> {
     err.statusCode= err.statusCode || 500
     err.status= err.status || 'error'
     res.status(err.statusCode).json({
          status:err.status,
          message:err.message
     })
})
Comment

express error middleware

function errorHandler (err, req, res, next) {
  if (res.headersSent) {
    return next(err)
  }
  res.status(500)
  res.render('error', { error: err })
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: simple alert program in javascript 
Javascript :: vue mapgetters with parameter 
Javascript :: remove part of string javascript 
Javascript :: youtube set speed command 
Javascript :: react native create apk 
Javascript :: stylesheet create 
Javascript :: array of images javascript 
Javascript :: usereducer react js 
Javascript :: react click outside 
Javascript :: how to add items in an array in js 
Javascript :: datatables server side 
Javascript :: send x-www-form-urlencoded request nodejs 
Javascript :: js check if undefined 
Javascript :: convert Float64Array to array in js 
Javascript :: javascript compare two arrays of objects 
Javascript :: convert arrow function to normal function javascript 
Javascript :: axios patch 
Javascript :: faker random from array 
Javascript :: winston logger creating particular log file for each level 
Javascript :: https error response with status 200 angular 
Javascript :: jquery on scroll 
Javascript :: react i18n outside component 
Javascript :: upload excel file using jquery ajax 
Javascript :: get url query in react 
Javascript :: disable button based on condition angular 
Javascript :: discord.js set playing tag 
Javascript :: p5js import typescript 
Javascript :: java script login system 
Javascript :: How to set the background image URL of an element using jQuery 
Javascript :: date js add days 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =