javascript - 运行 Node Express 服务器显示错误消息

标签 javascript express reactjs webpack

webpack 新手,尝试运行我自己的服务器 (node server.js),但我收到此消息:

$ node server.js
/path/to/server.js:3
import path from 'path'
^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (bootstrap_node.js:352:7)
    at startup (bootstrap_node.js:144:9)
    at bootstrap_node.js:467:3

我的 server.js:

require("babel-register");

import path from 'path'
import Express from 'express'
import React from 'react'
import { createStore } from 'redux'
import { Provider } from 'react-redux'

import counterApp from './reducers'
import App from './containers/App'

import { renderToString } from 'react-dom/server'

const app = Express()
const port = 3000

// This is fired every time a request is made
app.use(handleRender)


function handleRender(req, res) {
   // Create a new Redux store instance
   const store = createStore(counterApp) // this is taken from './reducers'

   // Render the component to a string
   const html = renderToString(
      <Provider store={store}> // Needs an import?
         <App /> // this is defined in './containers/...'
      </Provider>
   )

   // Grab the initial state from our Redux store
   const preloadedState = store.getState()

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

// Yet to be defined...
function renderFullPage(html, preloadedState) {
   return
      <!doctype html>
      <html>
         <head>
            <title>Paulos3000 Redux Universal Example</title>
         </head>
         <body>
            <div id='root'>${html}</div>
            <script>
               window.__PRELOADED_STATE__ = ${JSON.stringify(preloadedState)}
            </script>
            <script src='/static/bundle.js'></script>
         </body>
      </html>
}

app.listen(port)

有人能看出我的代码有问题吗?如果您需要我提供更多信息,请告诉我(不确定具体要包括什么,因为我不知道问题的根源)。

更新:

SyntaxError: /path/to/server.js: Unexpected token (25:6)
  23 |   
  24 |    const html = renderToString(
> 25 |       <Provider>
     |       ^
  26 |          <App />
  27 |       </Provider>
  28 |    )

最佳答案

根据您的 Node 版本,它不支持 import 语法。

尝试通过 require() 使用导入:

const path = require('path').default

关于javascript - 运行 Node Express 服务器显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40409999/

相关文章:

javascript - "all not first in a div preceded by a object with class"的 jQuery 选择器

javascript - 如何在 setState 回调之外将 .map() val 和 key 渲染为 prop

node.js - Node.js + Express 中的 split() 问题

javascript - 如何使用 HTML 元素属性将数据从 Blade.php 文件传递​​到 React 组件?

javascript - 选择选项子组件不会在使用 React js 提交的表单上重置为键 0

javascript - 单独处理同一实例的多个异常

javascript - d3.js 在排序数据后更新条形图标签

javascript - 在 Socket.IO 中, "io"cookie 有什么用?

javascript - Express gzip 压缩问题

javascript - 更新调用后额外的 GET API 调用