Webpack& Express热重新加载无法更新更改

时间:2018-03-14 02:21:58

标签: javascript node.js express webpack

我试图让我的节点服务器通过webpack进行热重新加载,但我无法完全正确地完成它。具体来说,当我进行更改时,webpack会重建我的包文件,所有内容都看起来像是在工作,但实际上并没有反映出更改。快速举例:

如果我向我的路线提出这样的请求:

app.route('/reload').get((req,res) => res.send("hey!"))

我在回复中收到了消息。但后来说我将消息更改为"测试",webpack输出:

Hash: bbc35083f3348a900df2
Version: webpack 3.11.0
Time: 530ms
                               Asset      Size  Chunks             Chunk Names
                           server.js    247 kB       0  [emitted]  main
0.2c807aa934f7a169bd08.hot-update.js   7.35 kB       0  [emitted]  main
2c807aa934f7a169bd08.hot-update.json  43 bytes          [emitted]  
[./api/modules/routeDefinitions.js] ./api/modules/routeDefinitions.js 6.97 kB {0} [built]
    + 91 hidden modules
[HMR] Updated modules:
[HMR]  - ./api/modules/routeDefinitions.js
[HMR] Update applied.

但是我仍然得到了原来的信息"嘿!"如果我再向该路线发出请求。

Webpack配置,使用命令webpack --config ./config/webpack.config.api.js

运行
const webpack           = require('webpack'),
      path              = require('path'),
      nodeExternals     = require('webpack-node-externals'),
      StartServerPlugin = require('start-server-webpack-plugin')

module.exports = {
  entry: [ 'webpack/hot/poll?1000', './api/index' ],
  watch: true,
  target: 'node',
  externals: [ nodeExternals({ whitelist: ['webpack/hot/poll?1000'] }) ],
  resolve: { extensions: ['.js', '.jsx', '.json'] },
  module: {
    rules: [
      { test: /\.jsx?$/, use: "babel-loader", exclude: /node_modules/ },
    ],
  },
  plugins: [
    new StartServerPlugin('server.js'),
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.DefinePlugin({
      "process.env": {
        BUILD_TARGET: JSON.stringify("server"),
        NODE_ENV: JSON.stringify("development")
      }
    }),
  ],
  output: {
    path: path.join(__dirname, '..', '.build'),
    filename: 'server.js'
  }
}

然后我的app应该从我的index.js文件中重新加载,该文件包含:

import http from 'http'
import app from './server'

const server  = http.createServer(app)

let currentApp = app

server.listen(3000)

// If the server should reload
if (module.hot) {
 module.hot.accept("./server", () => {

  // Remove the current server
  server.removeListener('request', currentApp)

  // Set a new instance
  server.on('request', app)

  // Store the new instance as the current instance
  currentApp = app

 })
}

有人看到这个有什么问题或者知道我哪里出错了吗?如果你需要,我很乐意发布更多代码,只是想保持帖子半短期大声笑。任何帮助表示赞赏!!!

0 个答案:

没有答案