nodeJs編寫(xiě)錯(cuò)誤處理中間件問(wèn)題
更新時(shí)間:2022年12月03日 15:18:00 作者:你的美,讓我癡迷
這篇文章主要介紹了nodeJs編寫(xiě)錯(cuò)誤處理中間件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
nodeJs編寫(xiě)錯(cuò)誤處理中間件
app.use(async(ctx, next) => { try { await next(); } catch (err) { console.log(err, "錯(cuò)誤機(jī)制"); ctx.status = err.status || err.statusCode; ctx.body = { message: err.message }; } })
koa-json-error 處理錯(cuò)誤機(jī)制:
const koajsonerror = require("koa-json-error"); app.use(koajsonerror({ ? ? postFormat: (err, { stack, ...rest }) => process.env.NODE_ENV === "production" ? rest : { stack, ...rest } }));
溫馨提示:放在最前面
Connect中間件之錯(cuò)誤處理中間件
范例
var connect = require('connect'); connect() .use(errorHandler()) .listen(3000); /* 錯(cuò)誤處理中間件函數(shù)必須接受四個(gè)參數(shù):err, req, res和 next */ /* 而常規(guī)的中間件只有三個(gè)參數(shù):req,res和next */ /* 當(dāng)Connect遇到錯(cuò)誤時(shí),它只調(diào)用錯(cuò)誤處理中間件 */ function errorHandler() { var env = process.env.NODE_ENV || 'development'; return function(err, req, res, next) { res.statusCode = 500; switch (env) { case 'development': res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(err)); break; default: res.end('Server error'); } } }
多個(gè)錯(cuò)誤處理中間件組件
var connect = require('connect'); var api = connect() .use(users) .use(pets) .use(errorHandler); var app = connect() .use(hello) .use('/api', api) .use(noPage) .use(errorPage) .listen(3000); // hello 中間件組件 function hello(req, res, next) { if (req.url.match(/^\/hello/)) { res.end('Hello World\n'); } else { next(); } } // users 中間件組件 var db = { users: [ {name: 'tobi'}, {name: 'loki'}, {name: 'jane'} ] }; function users(req, res, next) { var match = req.url.match(/^\/user\/(.+)/); if (match) { var user = db.users[match[1]]; if (user) { res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(user)); } else { var err = new Error('User not found'); err.notFound = true; next(err); } } else { next(); } } // pets 中間件組件 function pets(req, res, next) { if (req.url.match(/^\/pet\/(.+)/)) { foo(); } else { next(); } } // errorHandler 錯(cuò)誤處理中間件組件 function errorHandler(err, req, res, next) { console.error(err.stack); res.setHeader('Content-Type', 'application/json'); if (err.notFound) { res.statusCode = 404; res.end(JSON.stringify({error: err.message})); } else { res.statusCode = 500; res.end(JSON.stringify({error: 'Internal Server Error'})); } } // noPage 中間件組件 function noPage(req, res, next) { res.statusCode = 404; res.end('Not Found'); } // errorPage 錯(cuò)誤處理中間件組件 function errorPage(err, req, res, next) { res.end('Impossible'); }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
nodejs服務(wù)內(nèi)存泄露排查過(guò)程和優(yōu)化方法
在開(kāi)發(fā)和部署Node.js應(yīng)用程序時(shí),內(nèi)存泄露是一個(gè)常見(jiàn)的挑戰(zhàn),本文將探討如何對(duì)于一個(gè)陌生項(xiàng)目進(jìn)行內(nèi)存排查和優(yōu)化的方法,文章通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Express URL跳轉(zhuǎn)(重定向)的實(shí)現(xiàn)方法
Express是一個(gè)基于Node.js實(shí)現(xiàn)的Web框架,其響應(yīng)HTTP請(qǐng)求的response對(duì)象中有兩個(gè)用于URL跳轉(zhuǎn)方法res.location()和res.redirect(),使用它們可以實(shí)現(xiàn)URL的301或302重定向。2017-04-04websocket結(jié)合node.js實(shí)現(xiàn)雙向通信的示例代碼
本文主要介紹了websocket結(jié)合node.js實(shí)現(xiàn)雙向通信的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02node簡(jiǎn)單實(shí)現(xiàn)一個(gè)更改頭像功能的示例
本篇文章主要介紹了node簡(jiǎn)單實(shí)現(xiàn)一個(gè)更改頭像功能的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12解決Node.js包管理器安裝報(bào)錯(cuò)npm?ERR!?code?1的問(wèn)題
在開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要使用各種Node.js包來(lái)擴(kuò)展我們的應(yīng)用程序功能,這些包通常通過(guò)npm(Node.js包管理器)進(jìn)行安裝和管理,有時(shí)候我們可能會(huì)遇到一些關(guān)于npm的錯(cuò)誤,本文將詳細(xì)介紹如何解決這個(gè)問(wèn)題,并提供一個(gè)詳細(xì)的實(shí)例,需要的朋友可以參考下2024-03-03