Running a node express server using webpack-dev-server

后端 未结 4 861
梦如初夏
梦如初夏 2020-12-22 17:52

I\'m using webpack to run my react frontend successfully using the following config:

{
    name: \'client\',
    entry: \'./scripts/main.js\',
    output: {
         


        
4条回答
  •  一生所求
    2020-12-22 18:10

    Since webpack-dev-server is just a tiny express server with compile on change and hot reload.

    So, if you already got a express server for backend API, just merge the compile on change and hot reload into your express server.

    Then after take a look at the package.json of webpack-dev-server, i find the key is just webpack-dev-middleware

    const express = require('express'); //your original BE server
    const app = express();
    
    const webpack = require('webpack');
    const middleware = require('webpack-dev-middleware'); //webpack hot reloading middleware
    const compiler = webpack({ .. webpack options .. }); //move your `devServer` config from `webpack.config.js`
    
    
    app.use(middleware(compiler, {
      // webpack-dev-middleware options
    }));
    
    app.listen(3000, () => console.log('Example app listening on port 3000!'))
    

    So, when you run your BE server, it will compile all the things using webpack, and watch for changes, LOL ~

    Also, add webpack-hot-middleware for hot reloading function, see Hot Module Replacement

提交回复
热议问题