【发布时间】:2018-10-12 01:59:18
【问题描述】:
您好,我是 react 新手,我正在尝试使用 react 附加 express 框架,我按照本教程进行操作:https://blog.hellojs.org/setting-up-your-react-es6-development-environment-with-webpack-express-and-babel-e2a53994ade 但是当我运行服务器时出现以下错误:
加载资源失败:服务器响应状态为 404 (未找到) localhost/:1
拒绝执行脚本 'http://localhost:3000/dist/bundle.js' 因为它的 MIME 类型 ('text/html') 不可执行,严格的 MIME 类型检查是 已启用。
我已经搜索这个错误两天了,我还没有找到解决方案。我认为问题在于 webpack 没有创建 bundle.js,我现在想知道为什么会发生这种情况
我的项目目录如下:
我的配置 webpack 文件:
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: './client/index.js',
output: {
path: __dirname,
filename: 'bundle.js',
publicPath: '/client/assets/'
},
module: {
loaders: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
include: path.join(__dirname, 'client'),
exclude: /node_modules/,
query: {
presets: ['es2015', 'react', 'stage-0']
}
},
{
test: /\.css$/,
loader: 'css-loader'
}
]
},
};
server.js,我在其中创建 express 实例:
const path = require('path')
const express = require('express')
module.exports = {
app: function () {
const app = express();
const indexPath = path.join(__dirname, 'indexDep.html');
const publicPath = express.static(path.join(__dirname, '../dist'));
app.use('/dist', publicPath);
app.get('/', function (_, res) { res.sendFile(indexPath) });
return app;
}
}
还有 app.js,我在其中运行服务器:
const Server = require('./server.js')
const port = (process.env.PORT || 3000)
const app = Server.app()
if (process.env.NODE_ENV !== 'production') {
const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const config = require('../webpack.dev.config.js')
const compiler = webpack(config)
app.use(webpackHotMiddleware(compiler))
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPathdist
}))
}
app.listen(port)
console.log(`Listening at http://localhost:${port}`)
【问题讨论】:
-
在你的项目中寻找
bundle.js。如有必要,您可以使用 Atom 的模糊查找器执行 Ctrl+T/Cmd+T 来查找文件(是的,我认出了编辑器)。你在哪里看到它? -
/node_modules/ajv/scripts/bundle.js
-
这意味着什么?
-
不,不在那里。不包括
node_modules。 -
那么不行,我在任何地方都看不到它
标签: javascript reactjs webpack