我想使用.pug
和.jsx
作为快速视图引擎。
我尝试使用其他模块,例如@react-ssr/express
,但它们不起作用。
(看来我的项目是基于几年前最后更新的一个开源项目)
所以,我现在正在使用node-jsx
and react-engine
,但是这些模块不允许我使用window
and document
。
(就像这个截图)
我已经通过谷歌搜索尝试了许多解决方案,如下面的代码。
const React = require("react");
...
const Index = (props) => {
if (typeof window !== 'undefined') {
// code with document and window
}
}
module.exports = Index;
但是呈现了空白页面。
我认为这些模块只是渲染和运行我的 jsx 文件,而不需要任何转换为 javascript。
有没有办法使用window
anddocument
和node-jsx
and react-engine
?
(或者请向我推荐其他模块)
服务器.js
require("node-jsx").install({ extension: '.jsx', harmony: true });
const renderer = require("react-engine");
const Engine = renderer.server.create();
...
app.set('views', __dirname + "/views");
app.engine('jsx', Engine);
app.set('view engine', "pug");
app.set('view engine', "jsx");
索引.jsx
const React = require("react");
...
const Index = (props) => {
// code with document and window
}
module.exports = Index;