【问题标题】:webpack express Uncaught SyntaxError: Unexpected token <webpack express Uncaught SyntaxError: Unexpected token <
【发布时间】:2017-04-08 23:04:23
【问题描述】:

我需要帮助,出现此错误“webpack express Uncaught SyntaxError: Unexpected token

file struture:
app
package.json
server.js
webpack.config.js
-public
--index.html
--bundle.js
-node_modules
-src
--config.js
--index.js
--about
---about.html
--todos
---todos.html

webpack.config.js

var webpack = require('webpack');
var path = require('path');
module.exports = {
    devtool: 'inline-source-map',
    entry: ['./src'],
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    },
    resolve: {
        modulesDirectories: ['node_modules', 'src'],
        extension: ['', '.js']
    },
    module: {
        loaders: [{
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel',
            query: {
                presets: ['es2015']
            }},
        { test: /\.html$/,
            loader: 'raw'}
        ]
    },
    devServer: {
        hot: true,
        proxy: {
            '*': 'http://localhost:3000'
        }
    }
};

index.js

import angular from 'angular';
import appModule from 'config';
angular.bootstrap(document, [appModule.name]);

index.html

<html>
    <head>
        <title>MEAN ToDo App</title>
        <base href="/">
    </head>
    <body>
        <div ui-view></div>
        <script src="bundle.js"></script>
    </body>
</html>

config.js

import angular from 'angular';
import uiRouter from 'angular-ui-router';


const app = angular.module('app', [uiRouter]);

app.config(($stateProvider, $urlRouterProvider, $locationProvider) => {
    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('todos', {
            url: '/',
            template: require('todos/todos.html'),
        })
        .state('about', {
            url: '/about',
            template: require('about/about.html')
        });

    $locationProvider.html5Mode(true);
});

export default app;

server.js

var express = require('express');
var app = express();
var path = require('path');

var PORT = process.env.PORT || 3000;



app.all('/*', function(req, res) {
    res.sendFile(path.join(__dirname, 'public/index.html'));
});

app.listen(PORT, function() {
    console.log('Server running on ' + PORT);
});

【问题讨论】:

    标签: angularjs express webpack babeljs


    【解决方案1】:

    问题出在节点代码中:

    app.all('/*', function(req, res) {
       res.sendFile(path.join(__dirname, 'public/index.html'));
    });
    

    所有路由都返回index.html。尝试删除*

    【讨论】:

      【解决方案2】:

      您的 express 服务器配置错误 - 它总是返回 index.html

      试试这个:

      app.use(express.static('/'));
      
      app.listen(3000, function() {
        console.log('listening');
      });
      

      或者使用来自 npm 的http-server

      【讨论】:

        猜你喜欢
        • 2020-01-25
        • 2017-06-15
        • 2016-06-28
        • 2017-09-10
        • 2017-03-17
        • 2012-05-17
        • 2019-02-10
        • 2014-07-08
        • 2011-03-09
        相关资源
        最近更新 更多