我尝试使用express配置webpack。但是在浏览器cosole中的bundle.js中,我看到有与html文件中相同的代码,而不是预期的js。这是我的服务器配置:
const express = require('express');
const app = express();
const http = require('http');
const webpack = require('webpack');
const webpackConfig = require('./webpack/webpack.config');
const compiler = webpack(webpackConfig);
app.use(express.static(__dirname + '/'));
app.get(/.*/, function (req, res) {
res.sendFile(__dirname + '/index.html');
console.log(webpackConfig);
});
app.use(function(res, req, next) {
const err = new Error('You have 404 error!');
err.status = 404;
});
let server = http.createServer(app);
server.listen(3000, function onListening() {
const address = server.address();
console.log('This is port ' + address.port);
});
这是webpack:
require('babel-polyfill').default;
const path = require('path');
const common = {
entry: path.join(__dirname, '../src'),
output: {
path: path.join(__dirname, '../dist'),
filename: 'bundle.js',
publicPath: 'dist/',
library: "home"
},
watch: true,
watchOptions:{
aggregateTimeout: 100
},
module: {
loaders:[{
test: /\.js$/,
loaders: ['babel-loader'],
exclude: /node_modules/,
}]
}
}
module.exports = common;
index.html中的我包括这样的js:
src="/dist/bundle.js"
文件夹的结构是:
.
├── bin
│ └── index.js
├── index.html
├── package.json
├── server.js
├── src
│ └── index.js
└── webpack
└── webpack.config.js
答案 0 :(得分:-1)
是的,我们可以使用Html代替Js。因为它很容易理解,并且对入门级的学生没有混淆。