我使用this反应样板。它在快速Web服务器上运行。
我可以在服务器
中的JavaScript文件that进程中获取客户端IP地址但我想知道如何将该值传递给React Frontend脚本?
请告诉我一些事情。
更新 现在,我可以使用javascript模板引擎使用它,并在渲染之前替换IP。 这是代码
app.get('*', (req, res) => {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
if (err) {
res.sendStatus(404);
} else {
const compileStr = Mustache.render(file.toString(), { userIpAddress: ip });
res.send(compileStr);
}
});
});
然而,在制作中,它正在使用sendFile()
而我无法获得生成fs
模块: - (
const addProdMiddlewares = (app, options) => {
const publicPath = options.publicPath || '/';
const outputPath = options.outputPath || path.resolve(process.cwd(), 'build');
// compression middleware compresses your server responses which makes them
// smaller (applies also to assets). You can read more about that technique
// and other good practices on official Express.js docs http://mxs.is/googmy
app.use(compression());
app.use(publicPath, express.static(outputPath));
app.get('*', (req, res) => res.sendFile(path.resolve(outputPath, 'index.html')));
};