package.json文件中
"scripts": {
"test": "webpack-dev-server --hot",
"server":"nodemon server.jsx --exec babel-node"
},
server.jsx
具体代码为
import express from 'express'
import React from 'react'
import { renderToString } from 'react-dom/server'
import {RoutingContext,match} from 'react-router'
import {Provider} from 'react-redux'
import * as test from '../../actions/test/testaction'
import routes from './routes'
import configureStore from './stores/index.js'
const app=express()
function renderFullPage(html,initialState){
return (`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<div id="app">
<div>
${html}
</div>
</div>
<script>
window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};
</script>
<script src="index.js"></script>
</body>
</html>`
)
}
app.use((req, res) => {
match({ routes, location: req.url }, (err, redirectLocation, renderProps) => {
if (err) {
res.status(500).end(`Internal Server Error ${err}`);
} else if (redirectLocation) {
res.redirect(redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
const store = configureStore();
const state = store.getState();
Promise.all([
store.dispatch(test.getTestDate()) //store.dispatch(fetchItem(renderProps.params.id))
])
.then(() => {
const html = renderToString(
<Provider store={store}>
<RoutingContext {...renderProps} />
</Provider>
)
res.end(renderFullPage(html, store.getState()));
});
} else {
res.status(404).end('Not found');
}
});
});
这是自己按照教程写的一个小栗子,但是不知道为什么会出现这个错误
目测是缺少
babel-preset-react
,检查下.babelrc
吧。