Skip to content

How to add extra routes to the Vite HTTP server? (serve front and back end at the same time) #20025

Answered by adam-nielsen
adam-nielsen asked this question in Q&A
Discussion options

You must be logged in to vote

I ended up looking into plugins and found a quick simple solution:

export default defineConfig({
  plugins: [
    {
      name: 'api-handler',
      configureServer(server) {
        server.middlewares.use('/test', (req, res, next) => {
          res.writeHead(200, { 'Content-Type': 'text/plain' });
          res.end('test');
        });
      },
    },
    react(),
  ],
});

This allows me to handle that one custom URL, but leave everything else to React.

I extended this to handle the Lambda stuff, like so:

const lambdaShim = (url: string, handler: Function) => ({
  name: 'lambda-shim',
  configureServer(server: ViteDevServer) {
    server.middlewares.use(url, async (req, res) => {
     …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by adam-nielsen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant