我想將資料從我的config.js檔案發送到每個渲染。有沒有辦法用幾行代碼來做到這一點,而不必使用每個渲染函式手動推送資料?
這就是我的意思
const data = require("config.js").coolData;
app.get("/", (req, res) => {
res.render("index", {data});
});
app.get("/request2", (req, res) => {
res.render("index", {data);
});
app.get("/request3", (req, res) => {
res.render("index", {foo, data);
});
有沒有辦法輕松發送data給所有三個?
uj5u.com熱心網友回復:
如果您使用的是 express,則可以res.locals;在中間件中使用例如:
const data = require("config.js").coolData;
app.use((req, res, next) => {
res.locals.data = data;
next();
});
...
只要確保將中間件放在任何其他路由之前
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312249.html
