當(dāng)我還嘗試使用動態(tài)參數(shù)捕獲 url 時如何處理 404 頁面?IE,/:id下面的代碼永遠不會達到 404 的最后一條規(guī)則,html即使文件不存在,Express也會嘗試打開 它們。const express = require('express');const app = express();app.use(express.static(__dirname + '/dev'));app.get('/', function(req, res) { res.sendFile(`${__dirname}/dev/index.html`);});app.get('/:id', function(req, res) { res.sendFile(`${__dirname}/dev/pages/${req.params.id}.html`);});app.get('*', function(req, res) { res.status(404).sendFile(`${__dirname}/dev/404.html`);});app.listen(3000, function() { console.log('Express server started http://localhost:3000');});
Express.js - 使用路由參數(shù)時如何處理 404 頁面
森林海
2021-10-21 16:16:20