1

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 Answer 1

3

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))
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.