question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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)

  1. Run vite or vite build with the --debug flag.
  2. Provide the error log here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
yyx990803commented, Jan 20, 2021

fixed in 85c89be

0reactions
hpx7commented, Jan 20, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found