Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a static folder in Koa?

index.js: (placed inside of server folder.)

const serve = require('koa-static')
const Koa = require('koa');
const app = new Koa();

app.use(serve(__dirname + './public'));

app.listen(3000);

console.log('listening on port 3000');

I want to show index.html, which is located in /public folder.

When I start index.js above, I see Not Found on the browser.

console.log shows, it's referring to same folder where index.js is.

UPDATE 1:

console.log(__dirname + './public'); shows

/Users/askar/work/react/hub/server./public,

but I need /Users/askar/work/react/hub/public

like image 818
Askar Avatar asked Oct 18 '25 12:10

Askar


1 Answers

Solution:

Changed from app.use(serve(__dirname + './public'));

To app.use(serve('./public'));

Reference: https://www.tutorialspoint.com/koajs/koajs_static_files.htm

like image 144
Askar Avatar answered Oct 21 '25 02:10

Askar