我正在为 CRA(create-react-app) 反应项目创建 SSR 服务器。但是,当我执行“npx webpack”时出现错误。
这是我的目录树的图片。 我的文件目录
服务器.js
import React from 'react';
import {renderToString} from "react-dom/server";
import express from 'express';
import App from '../src/App.js';
const doc = content => `
<!doctype html>
<html>
<head>
<title>Rendering to strings</title>
</head>
<body>
<div id="app">${content}</div>
</body>
</html>`;
const app = express();
app.get('/', (req,res)=>{
const props = {
items: ['One','Two','Three']
};
const rendered = renderToString(<App {...props}/>);
res.send(doc(rendered));
})
app.listen(8080, ()=>{
console.log(`listening on localhost:8080`);
})
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
const path = require('path');
module.exports = {
entry: './server.js', …Run Code Online (Sandbox Code Playgroud)