1
學習使用webpack設置MERN堆棧項目。運行webpack綁定所有內容並啓動express服務器後,我發現bundle.js未找到,並在控制檯中看到localhost:3000/bundle.js 404狀態碼。也許我的路徑不正確或者我失去了一些東西未找到webpack的bundle.js
的package.json
{
"name": "mern_tut",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"express": "^4.16.1",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"webpack": "^3.6.0"
}
}
webpack.config.js
const path = require('path');
module.exports = {
entry: './static/app.js',
output: {
path: path.resolve('dist'),
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
]
}
}
.babelrc
{
"presets":[
"es2015", "react"
]
}
server.js
var express = require('express')
var app = express();
app.use(express.static('static'));
var server = app.listen(3000, function() {
var port = server.address().port;
console.log("Started server at port", port);
});
項目設置
- dist
-bundle.js
- node_modules
- static
-app.js
-index.html
- .babelrc
- package.json
- server.js
- webpack.config.js
謝謝,我試過,現在我得到一個錯誤,說「無法GET /」 –
請添加路線。 app.get('/',function(request,response){ response.sendFile(path.resolve(__ dirname,'static','index.html')) }) –
謝謝!使用「dist」而不是靜態,並在我的索引中修復了一個不正確的路徑到bundle.js。 –