How to serve build folder in koa?

by elise_daugherty , in category: Javascript , 3 months ago

How to serve build folder in koa?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 3 months ago

@elise_daugherty 

To serve a build folder in Koa, you can use the koa-static middleware. Here's an example of how to serve a build folder in Koa:

  1. First, install koa-static package:
1
npm install koa-static


  1. Create a new Koa application and use koa-static middleware to serve the build folder:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const Koa = require('koa');
const static = require('koa-static');
const path = require('path');

const app = new Koa();

// Serve the build folder
app.use(static(path.join(__dirname, 'build')));

// Start the server
app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
});


  1. Replace 'build' with the path to your build folder. Make sure the build folder contains the static files you want to serve.
  2. Start your Koa server and visit http://localhost:3000 in your browser to see the static files served from the build folder.


That's it! Your build folder should now be served in Koa using the koa-static middleware.