vue-cli devServer proxy bypass

1.2k views Asked by At

I would like Vue CLI's dev server to return a string when a specific URL is fetched. For this, I wanted to use webpack dev-server's bypass option. (webpack docs)

I tried this:

devServer: {
    proxy: {
        '/something': {
            bypass: (req, res) => res.send(process.env.SOMETHING),
        }
    }
}

This causes an error: When proxy in package.json is an object, each context object must have a target property specified as a url string.

I don't need a target option (like in this example)

How can I make this work?

1

There are 1 answers

0
tony19 On BEST ANSWER

devServer.proxy isn't appropriate for this. Instead, add your route in devServer.before in Webpack 4 (used in Vue CLI 4):

// vue.config.js
module.exports = {
  devServer: {
    before(app) {
      app.get('/something', (req, res) => res.send(process.env.SOMETHING))
    }
  }
}

...or devServer.onBeforeSetupMiddleware in Webpack 5 (used in Vue CLI 5):

// vue.config.js
module.exports = {
  devServer: {
    onBeforeSetupMiddleware(server) {
      server.app.get('/something', (req, res) => res.send(process.env.SOMETHING))
    }
  }
}