运行webpack-dev-server 页面报错cannot get /

问题描述:项目运行npm start 浏览器始终报cannot get / ,webpack打包过程没有任何问题。

webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

let getHtmlConfig = function (name, title) {
    return {
        template: './demos/' + name + '.html',
        filename: 'demos/' + name + '.html',
        title: title,
        inject: true | 'body',
        hash: true,
        cache: true,
        chunks: ['common', name]
    };
};

module.exports = {

    entry: {
        'a' : './js/a.js',
        'b' : './js/b.js',
        'c' : './js/c.js'
    },

     output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/[name].min.js',
    },

    module: {
        loaders: [{
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            }, 
            {
                test: /\.css$/,
                use: ['css-loader']
            },
            {
                test: /\.(png|jpg|gif|jpeg)$/,
                loader: 'url-loader',
                options: {
                    limit: 2048,
                    name: 'images/[name].[ext]',
                    publicPath:'../'
                }
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2|otf)$/,
                loader: 'url-loader',
                options: {
                    limit: 8192,
                    name: 'fonts/[name].[ext]'
                }
            }
        ]
    },

    plugins: [
        new HtmlWebpackPlugin(getHtmlConfig('a', 'a')),
        new HtmlWebpackPlugin(getHtmlConfig('b', 'b')),
        new HtmlWebpackPlugin(getHtmlConfig('c', 'c')),
        new ExtractTextPlugin('css/[name].min.css')
    ],

    devServer: {
        host: '0.0.0.0',
        useLocalIp: true,
        contentBase:path.resolve(__dirname, 'dist'),
        compress: true,
        port: 9000,
        open: true,
    }
};

package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "webpack --watch",
    "start": "webpack-dev-server --open",
    "build": "webpack --env.production"
  },
阅读 8.7k
1 个回答

这个貌似不是问题吧。由于你是多页的应用,所以浏览器默认打开的 index.html 并不存在。你可以试试 http://localhost:9000/demos/a.html#/你的主页 能否访问

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题