Vite middleware not working
See original GitHub issue⚠️ IMPORTANT ⚠️ Please check the following list before proceeding. If you ignore this issue template, your issue will be directly closed.
- Read the docs.
- Use Vite >=2.0. (1.x is no longer supported)
- If the issue is related to 1.x -> 2.0 upgrade, read the Migration Guide first.
Describe the bug
Vite server as middleware unable to serve index.html
on GET /
Reproduction
In server.ts
:
const app = express();
const server = http.createServer(app);
const vite = await createServer({ server: { middlewareMode: true } });
app.use(vite.middlewares);
server.listen(3000);
Visiting localhost:3000 in the browser results in a 404 (Cannot GET /
), even though there is a index.html
file in the same directory as the server
System Info
vite
version: 2.0.0-beta.34- Operating System: osx
- Node version: 15.6.0
- Package manager (npm/yarn/pnpm) and version: npm 7.4.0
Logs (Optional if provided reproduction)
- Run
vite
orvite build
with the--debug
flag. - Provide the error log here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Add Express middleware function for Vite server #258 - GitHub
For simple projects I like to integrate the local dev server with the API server. I still use Express, and with Vite using...
Read more >vite dev server execute middleware before all other middleware
According to this github issue, environment variables are not accessible in file vite.config.js (neither in vite.config.ts ).
Read more >Server-Side Rendering - Vite
Here vite is an instance of ViteDevServer. vite.middlewares is a Connect instance which can be used as a middleware in any connect-compatible Node.js...
Read more >Server Options - Vite
If not in middleware mode, Vite will attempt to process HMR connection requests through the existing server. This can be helpful when using...
Read more >Troubleshooting - Vite
Vite cannot handle and does not support code that only runs on non-strict mode (sloppy mode). This is because Vite uses ESM and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
fixed in 85c89be
Thanks @yyx990803, I was able to get it working. One limitation that I would note with this approach is that HMR doesn’t work properly on
index.html
– I see a{type: "full-reload", path: "/index.html"}
websocket message but it’s not accompanied by a full page refresh.