简体   繁体   English

发送html而不是js

[英]Sending html instead of js

I try to configure webpack with express. 我尝试用express配置webpack。 But in bundle.js in browser cosole, I see that there is the same code as in html file, not js as expected. 但是在浏览器控制台中的bundle.js中,我看到与html文件中的代码相同,而不是预期的js。 This is my server configuration: 这是我的服务器配置:

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);
});

And this is webpack: 这是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;

in index.html I include js like this: 在index.html中,我包括这样的js:

src="/dist/bundle.js"

The structure of folders is: 文件夹的结构为:

.
├── bin
│   └── index.js
├── index.html
├── package.json
├── server.js
├── src
│   └── index.js
└── webpack
    └── webpack.config.js

Yes we can use Html instead of Js. 是的,我们可以使用HTML代替Js。 Because it's easy to understand and there is no confusion for the students who are at entry level. 因为它易于理解,入门级的学生不会感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM