"Unexpected token <" when rendering side to side

Recently, I tried to create a universal app, using the express+ react+redux

here is a brief description of my problem:

I successfully processed the HTML reaction components and sent them to the client using express res.send(renderFullPage(html, initialState)) . html displays correctly on the browser side. However, I kept getting this error in the browser console:

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

when I go to the problem by pressing bundle.js: 1 , I saw:

<!doctype html> (x)

I start the node server by running the command npm run devServerin 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')
})

`` ``

here is my function 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>
  `
}

`` ``

- , ?

+4

All Articles