37

I am using webpack-dev-server to act as a CDN server locally to serve various static assets like css, js, html etc.

Everything runs fine but for debugging purposes, I am unable to see the requests received by the CDN server.

webpack-dev-server just goes silent and doesn't show any info/errors once it has compiled the bundle of static assets.

I went through the command line help too but with no success.

comiventor
  • 3,922
  • 5
  • 50
  • 77

2 Answers2

73

Basically, webpack-dev-server uses express to spawn a webserver. To enable seeing logs, one needs to set DEBUG environment variable as required by express

export DEBUG='express:*'

This started showing me logs of various requests received by webpack-dev-server.

To disable the logs, set the environment variable empty again

export DEBUG=

This works even for loopback projects and any other NodeJS frameworks which use express as base.

comiventor
  • 3,922
  • 5
  • 50
  • 77
5

the environment variable didn't worked for me.

According to the docs, you can add your own custom middleware via before (add this to vue.config.js):

devServer: {
    disableHostCheck: true,
    host: 'localhost',
    https: {
        key: fs.readFileSync('./dev/ssl-localhost-testing/server.key'),
        cert: fs.readFileSync('./dev/ssl-localhost-testing/server.cert'),
    },
    before: function (app, server, compiler) {
        app.use('/', function (req, res,next) {
            console.log(`from ${req.ip} - ${req.method} - ${req.originalUrl}`);
            next();
        });
    }
 }

This logs each request to the console like:

from 127.0.0.1 - GET - /js/BACC_AtlasAdministration.js

Maybe this helps somebody

Chris
  • 2,296
  • 4
  • 27
  • 46
  • 3
    with `webpack-dev-server` version 4 `before(app, server, compiler)` became `onBeforeSetupMiddleware(devServer)` and `app.use` must be changed to `devServer.app.use` – aleneum Sep 11 '21 at 10:08
  • Which is now deprectaed in favour of `setupMiddlewares`: https://webpack.js.org/configuration/dev-server/#devserversetupmiddlewares – Jean Claveau Jan 25 '22 at 11:13