允许从Express中的不同文件访问variables
我有2个Javascript文件。 一个app.js
文件和productController.js
。
我在app.js
设置了一个variables,需要在productController.js
访问它
....... const next = require('next') const dev = process.env.NODE_ENV !== 'production' const nextLoader = next({dev}) const handle = nextLoader.getRequestHandler() nextLoader.prepare().then(() => { const app = express(); app.use(cors()); app.set('nextRender', nextLoader) <----- .......
然后在productController.js
即时通讯尝试使用我在app.js
文件中设置的nextRender
。
const Product = require('../models/Product.js') exports.listProducts = (req, res) => { nextRender.render(req,res, '/products') }
在app.js
,将你的nextLoader
对象存储在app
:
app.nextRender = nextLoader;
在productionController.js
使用req.app.nextRender
:
exports.listProducts = (req, res) => { req.app.nextRender.render(req, res, '/products') }