How to get the runtime host:port in vite-plugin hooks? #7917
Replies: 5 comments
-
|
Is anyone else paying attention to this issue? |
Beta Was this translation helpful? Give feedback.
-
I' want to implement a fixtures directory when a request visit and then proxy to another directory. |
Beta Was this translation helpful? Give feedback.
-
|
you can use http-server middleware, such as export function proxyToFile( fileProxyMap: { [visitPath: string]: string}) {
return {
name: 'vite-plugin-proxy-to-file',
configureServer(server) {
const proxyVisitPathList = Object.keys(fileProxyMap)
sever.middlewares.use((req, _res, next) => {
const visitPath = proxyVisitPathList.find(visitPath => req.url.includes(visitPath)) // you can modify the condition, here using `includes`, can also be `startsWith`, `equals` or RegExp etc.
if (visitPath) {
req.url = req.url.replace(visitPath, fileProxyMap[visitPath]) // add your own logic
}
next()
});
}
}
}and apply it to your vite config export default defineConfig({
//...,
plugins: [
//....,
proxyToFile({
'/api/users': '/fixtures/api/users.json'
})
],
//....
})then the proxy works without knowing the real runtime port At present, there is no way to know the runtime port in advance |
Beta Was this translation helpful? Give feedback.
-
|
You could e.g. "dev": "export DEV_PORT=5173; vite --port $DEV_PORT",then access |
Beta Was this translation helpful? Give feedback.
-
|
I don't think you can know the final runtime port inside the So in That means the answer is basically:
The docs point to So my read is:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
When I want to mod
devServer.proxyconfigs in vite's config hook, it is confusing that I can't get host:port which will be used when devServer running.http://localhost:3000is obviously not alaways working, the port may be changed to 3001/3002/3003...etc.So how to get the host:port of the devServer in config hook??
Beta Was this translation helpful? Give feedback.
All reactions