PositiveGuy
PositiveGuy

Reputation: 20212

Serve up Static Image Koa.js

I'm returning a value in JSON for example in one of my endpoints:

{
    "image":"http//devservername/assets/someimage.jpg"
}

I'm trying to figure out how to statically serve this through Koa.js and not sure if that's going to be in a route or what...

Upvotes: 1

Views: 3228

Answers (2)

user663031
user663031

Reputation:

Use koa-static.

npm install --save koa-static

// index.js or whatever
var KoaStatic = require('koa-static');
app.use(KoaStatic('assets'));

This will serve a request for foo.txt from assets/foo.txt. If you want to serve assets/foo.txt in response to a request for assets/foo.txt, then call KoaStatic('.'). That is probably not a good idea, since it will serve anything from the root. Better to create a public directory, put an assets directory under it, and use KoaStatic('public').

Upvotes: 4

Rei Dien
Rei Dien

Reputation: 196

remove http://devservername and it will link, ofcourse you should use middleware to serve folders.

use koa-static-folder

import serve from 'koa-static-folder';

serve(__dirname + '/public/');

Upvotes: 0

Related Questions