提问人:Majid Jamili 提问时间:9/1/2023 更新时间:9/1/2023 访问量:14
更新不同路由节点 js express 中的 Res Local 值
Update Res Local value in different routes node js express
问:
我是 node.js 和 express 的新手,我很难更新我的响应本地值,我想稍后在其他路由上更新myVariable
const express = require('express');
const app = express();
const port = 3000;
app.use((req, res, next) => {
res.locals.myVariable = 'Initial Value';
next();
});
const updateRoute = require('./routes/updateRoute');
const accessRoute = require('./routes/accessRoute');
app.use('/update', updateRoute);
app.use('/access', accessRoute);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
/updateRoute.js
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.locals.myVariable = 'Updated Value';
res.send('Value updated.');
});
module.exports = router;
/accessRoute.js
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
const message = res.locals.myVariable;
res.send(`Value: ${message}`);
});
module.exports = router;
我想在调用GET请求时更新值,因此一旦调用GET请求,例如,请看到更新值而不是初始值。有什么快速的解决方案吗?myVariable
/update
/access
答: 暂无答案
评论