import React from "react" const Home = () => { return <div>Home1233333</div> } export default Home;
import React from 'react'; import { renderToString } from 'react-dom/server' import Home from './containers/Home' app.get("/",(req,res)=>{ res.send(renderToString(<Home />)) })
var home = renderToString(<Home />); app.get("/",(req,res)=>{ res.send(`<html> <head> <title>服務端渲染</title> </head> <body> ${home} </body> </html>`) })
"start": "nodemon --watch build --exec node \"./build/bundle.js\"",
"build": "webpack --config webpack.server.js --watch"
nodemon --watch參數表示監聽的對象 --exec參數表示對象變化以後執行的命令 後面的文件路徑須要加雙引號,因爲外層有引號,使用 \ 進行轉義html
"dev": "npm-run-all --parallel dev:**", "dev:start": "nodemon --watch build --exec node \"./build/bundle.js\"", "dev:build": "webpack --config webpack.server.js --watch"
項目地址:git@github.com:longlongdan/Reactssr.gitnode