Webpack main.js错误"意外的令牌<"从快递服务器提供服务时

时间:2018-01-26 19:18:29

标签: node.js reactjs webpack

我试图通过捕获所有路由从快速服务器提供我的反应应用程序,以便用户在刷新页面或手动输入网址时仍然可以访问反应路由。但是,当我尝试从我的快速服务器提供应用入口点/dist/index.html时,我得到Uncaught SyntaxError: Unexpected token <

当我查看控制台的sources选项卡中的main.js文件时,可以看到:

<!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>React E-Store</title>
    </head>
    <body>
      <div id="root">
      </div>
    </body>
    <script src="/dist/main.js"></script>
  </html>

这是我的server.js文件的样子:

const express = require('express')
const path = require('path')
const port = process.env.PORT || 8080
const app = express()

// Handles all routes so you do not get a not found error
app.get('/*', function (request, response){
    response.sendFile(path.resolve(__dirname, "dist", "index.html"))
})

app.listen(port)
console.log("server started on port " + port)

这是我的webpack.config.js

const path = require("path");

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './index.html',
  filename: 'index.html',
  inject: 'body'
})

module.exports = {
  entry: './src/app.js',
  output: {
    path: path.resolve('dist'),
    publicPath: '/dist/',
    filename: '[name].js'
  },
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
      {
        test: /\.(png|jp(e*)g|svg)$/, 
            loader: 'url-loader',
            options: { 
                limit: 8000, // Convert images < 8kb to base64 strings
                name: 'images/[hash]-[name].[ext]'
            } 
     },
     {
        test: /\.sass$/, 
        use: [{
            loader: "style-loader" // creates style nodes from JS strings
        }, {
            loader: "css-loader" // translates CSS into CommonJS
        }, {
            loader: "sass-loader" // compiles Sass to CSS
        }]}
    ]
  },
  plugins: [HtmlWebpackPluginConfig]
}

我感谢任何帮助

0 个答案:

没有答案