gpt4 book ai didi

javascript - "Unexpected token < "在做 react redux 服务器端渲染时

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:41:23 31 4
gpt4 key购买 nike

我最近尝试使用 express 构建一个通用应用程序+ react + redux

这里是我的问题的简要描述:

我已经成功地将 React 组件呈现为 HTML 并通过 expressres.send(renderFullPage(html, initialState)) 将其发送回客户端. html 在浏览器端正确呈现。但是,我一直在浏览器控制台中收到此错误:


Uncaught SyntaxError: Unexpected token < ... bundle.js: 1`enter code here`

当我通过单击 bundle.js: 1 进一步研究问题时,我看到了:


<!doctype html> (x)

我通过运行 npm run devServer 来启动节点服务器package.json 中的命令:

```

{
"name": "universal",
"version": "1.0.0",
"author": "Bryan Huang",
"description": "nodejs isomorphic package",
"main": "index.js",
"scripts": {
"test": "mocha --compilers js:babel-core/register --recursive",
"devServer": "nodemon server/bin/server.js",
"buildServer": "babel server/server.js -d build",
"startServer": "node build/server/server.js",
"lint": "eslint . --ext .js --ext .jsx",
"build": "webpack",
"dev": "webpack-dev-server"
}
...
}

```

server/bin/server.js:

```

const fs =  require('fs')
const path = require('path')
const config = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../..', '.babelrc'), 'utf-8'))
require('babel-register')(config)
require(path.resolve(__dirname, '../server.js'))

```

server/server.js

```

import 'babel-polyfill'
import path from 'path'
import React from 'react'
import { renderToString } from 'react-dom/server'
import renderFullPage from './utils/render'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import App from '../src/containers/App'
import reducers from '../src/reducers'
import express from 'express'

const app = express()

const staticPath = path.resolve(__dirname, '../..', 'static')

// serve static files.
app.use('/static', express.static(staticPath))

// Fired everytime the server side receives a request.
app.use(handleRender)
app.use(renderFullPage)

function handleRender (req, res) {
// Create store.
const store = createStore(reducers)

// Render the component to string.
const html = renderToString (
<Provider store={store}>
<App />
</Provider>
)

// Get the initial state.
const initialState = store.getState()

// Send the rendered page back to client
res.send(renderFullPage(html, initialState))
}


app.listen(3004, () => {
console.log('listening in port 3004')
})

```

这是我的 renderFullPage功能:

```

export default function renderFullPage (html, initialState) {
return `<!doctype html>
<html>
<head>
<title>Universal App</title>
</head>
<body>
<div id='app'>${html}</div>
<script>
window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}
</script>
<script src='build/bundle.js'></script>
</body>
</html>
`
}

```

我是否做错了什么导致了这个问题?

最佳答案

为脚本标签“<script src="">code goes here.</script>'”添加一个空的 src 属性

引用:- http://chronicles.blog.ryanrampersad.com/2008/09/syntax-error-break-on-this-error-doctype-html-public-w3cdtd-xhtml3orgtr-xhtml1-dtd-xhtml1-strictdtd/

关于javascript - "Unexpected token < "在做 react redux 服务器端渲染时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37905952/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com