分享 5 個和 NodeJS 相關的高級技巧

const route = express.Router();const {login} = require("../controllers/auth");route.get('/login', login)// isAuthenticated is middleware that checks whether// you are authenticated or not// // ? Avoid this: middleware on each routeroute.get('/products', isAuthenticated, fetchAllProducts);route.get('/product/:id', isAuthenticated, getProductById)
// ? Instead, do this// Route without middlewareroute.get('/login', login)// Middleware function: isAuthenticated// This will be applied to all routes defined after this pointroute.use(isAuthenticated);// Routes that will automatically check the middlewareroute.get('/products', fetchAllProducts);route.get('/product/:id', getProductById);
這種方法有助于保持代碼的組織性,并避免為每個路由單獨重復中間件。
2.使用全局錯誤處理
我們可以使用 NodeJS 全局錯誤處理功能,而不是在每個控制器上構(gòu)建錯誤響應。首先,創(chuàng)建一個派生自內(nèi)置 Error 類的自定義 AppError 類。此自定義類允許您使用 statusCode 和 status 等附加屬性來自定義錯誤對象。
// Custom Error classmodule.exports = class AppError extends Error {constructor(message, statusCode) {super(message);this.statusCode = statusCode;this.status = statusCode < 500 ? "error" : "fail";Error.captureStackTrace(this, this.constructor);}};
創(chuàng)建自定義錯誤類后,請在根路由器文件中添加全局錯誤處理程序中間件。該中間件函數(shù)采用四個參數(shù)(err、req、res、next)并處理整個應用程序中的錯誤。
在全局錯誤處理程序中,您可以根據(jù)錯誤對象的 statusCode、status 和 message 屬性來格式化錯誤響應。
您可以自定義此響應格式以滿足您的需求。此外,還包括用于開發(fā)環(huán)境的堆棧屬性。
// Express setupconst express = require('express');const app = express();app.use('/', (req, res) => {res.status(200).json({ message: "it works" });});app.use('*', (req, res) => {res.status(404).json({message: `Can't find ${req.originalUrl} this route`,});});// ?? add a global error handler after all the routes.app.use((err, req, res, next) => {err.status = err.status || "fail";err.statusCode = err.statusCode || 500;res.status(err.statusCode).json({status: err.status,message: transformMessage(err.message),stack: process.env.NODE_ENV === "development" ? err.stack : undefined,});});
添加后,您可以使用 next(new AppError(message, statusCode)) 拋出錯誤。下一個函數(shù)會自動將錯誤傳遞給全局錯誤處理程序中間件。
// inside controllers// route.get('/login', login);exports.login = async (req, res, next) => {try {const { email, password } = req.body;const user = await User.findOne({ email }).select("+password +lastLoginAt");if (!user || !(await user.correctPassword(password, user.password))) {// ?? like thisreturn next(new AppError("Invalid Email / Password / Method", 404));}// Custom logic for generating a tokenconst token = 'generated_token';res.status(200).json({ token });} catch(error) {next(error}});
總體而言,這種方法通過將錯誤處理集中在一個位置來簡化錯誤處理,從而更輕松地在應用程序中維護和自定義錯誤響應。
3.使用自定義Try-Catch函數(shù)
我們可以使用實現(xiàn)相同目的的自定義函數(shù),而不是使用 try-catch 塊手動包裝每個控制器函數(shù)。
// ? Avoid this// Using try-catch block each controllersexports.login = async (req, res, next) => {try {// logic here} catch(error) {res.status(400).json({ message: 'You error message'}}});
tryCatchFn 函數(shù)接受函數(shù) (fn) 作為輸入,并返回一個用 try-catch 塊包裝原始函數(shù)的新函數(shù)。
如果在包裝函數(shù)內(nèi)發(fā)生錯誤,則使用 catch 方法捕獲錯誤,并將錯誤傳遞到下一個函數(shù)以由全局錯誤處理程序處理。
// ? Instead, do thisconst tryCatchFn = (fn) => {return (req, res, next) => {fn(req, res, next).catch(next);};}// To use this custom function, you can wrap your controller// functions with tryCatchFn:exports.login = tryCatchFn(async (req, res, next) => {// logic here});
通過使用 tryCatchFn 包裝控制器函數(shù),您可以確保自動捕獲這些函數(shù)中引發(fā)的任何錯誤并將其傳遞給全局錯誤處理程序,從而無需單獨添加 try-catch 塊。
這種方法有助于以更清晰、更簡潔的方式集中錯誤處理,使代碼更易于維護并減少重復的錯誤處理代碼。
4. 將主文件分成兩部分。
使用 Express 開發(fā) NodeJS 應用程序時,通常有一個包含所有業(yè)務邏輯、路由定義和服務器設置的主文件。
然而,隨著應用程序的增長,管理和維護處理所有事情的單個文件可能會變得困難。
解決此問題并保持代碼庫更干凈、更有條理的一種推薦技術是將主文件分為兩部分:一個用于路由,另一個用于服務器設置或配置。
這是一個例子:
// app.jsconst express = require('express');const app = express();/* Middlewares */app.get('/', (req, res) => {res.status(200).json({ message: "it works" });})app.use(/* Global Error Handler */);module.exports = app;// server.jsconst app = require('./app');const port = process.env.PORT || 5001;app.listen(port, () => console.log('Server running at', port));
5. 將路由與控制器分開
為了實現(xiàn)更有組織性和模塊化的代碼庫,我建議將路由與控制器分開。這種做法有助于保持清晰的關注點分離,并提高代碼的可讀性和可維護性。
這是一個演示路由和控制器分離的示例。
// ? Avoid thisconst route = express.Router();route.get('/login', tryCatchFn(req, res, next) => {// logic here}))// ? Do thisconst route = express.Router();const {login} = require("../controllers/auth");route.get('/login', login);
結(jié)論
在本文中,我們討論了編寫干凈且易于維護的 NodeJS 代碼的不同高級技術。有許多最佳實踐可以顯著提高應用程序代碼的質(zhì)量。
最后,希望這篇內(nèi)容對你有用,感謝你的閱讀。
