如果我使用像 express 这样的节点服务器,我是否需要 webpack-dev-server

Do I need webpack-dev-server if I am using a node server like express

我正在按照一些教程使用 express 和 react 构建同构应用程序。我对 webpack-dev-server 很困惑。

webpack 教程中提到了 webpack-dev-server:

This binds a small express server on localhost:8080 which serves your static assets as well as the bundle (compiled automatically).

It automatically updates the browser page when a bundle is recompiled (socket.io). Open http://localhost:8080/webpack-dev-server/bundle in your browser.

既然我有 express server,我真的需要 webpack-dev-server 吗?或者使用它的优点和缺点是什么?如果我想使用 react-hot-loader,是否需要 webpack-dev-server?

Since I have express server, do I really need webpack-dev-server?

是也不是。您可以使用混合方法,该方法实质上将 webpack-dev-server 设置为代理。你有你的 express 服务器,可以服务除资产之外的所有内容。如果它是资产,请求将 forwarded/proxied 发送到 webpack-dev-server。有关详细信息,请参阅此处的答案:How to allow for webpack-dev-server to allow entry points from react-router

或者,您可以使用 webpack-dev-middleware and webpack-hot-middleware instead if you don't want to deal with all the messy proxying logic and having 2 servers running. See the example here: https://github.com/glenjamin/webpack-hot-middleware/blob/master/example/server.js

what's the advantages and disadvantages of using it?

实时重载和热模块更换。我认为对开发非常有用的功能

And if I want to use react-hot-loader, is the webpack-dev-server necessary?

不,它在 Webpack 的 hot module replacement interface. You can create your own 'hot server' if you want. The webpack-dev-server client just listen to socket.io for hot updates and calls postMessage (https://github.com/webpack/webpack-dev-server/blob/8e8f540b2f7b35f7b6c3ce616a7fd2215aaa6eea/client/index.js#L64-L67) which is then picked up by the server https://github.com/webpack/webpack/blob/bac9b48bfb0f7dd9732f2faafb43ebb22ee2a2a7/hot/only-dev-server.js#L59-L67.

之上工作

或者我推荐的是,你可以直接使用我上面提到的 webpack-dev-middleware 和 webpack-hot-middleware 来代替。这样,您就不必担心代理逻辑​​,您可以轻松地将热重新加载集成到现有的快速服务器中,而无需 webpack-dev-server