14

The docs at https://github.com/koajs/static and my personal experience trying out koa-static lead me to believe that you can only serve files from the root URL of your app.

For example:

app.use(serve('./some/dir/'));

Given the above use of serve, the URL to access a file ./some/dir/something.txt would be localhost:3000/something.txt. There doesn't seem to be a way to configure my app such that the same file (and all other files in the same dir) is served at localhost:3000/static/something.txt instead.

I'm new to Node and to Koa, so I've just begun to dive into this and I'm probably missing something really obvious.

I tried using koa-route to achieve this:

app.use(route.get('/static/*'), serve(__dirname + '/some/dir'));

But upon requesting /static/something.txt I was met with the following:

  TypeError: Cannot read property 'apply' of undefined
      at Object.<anonymous> (/Users/me/example/src/node_modules/koa-route/index.js:34:18)
      at GeneratorFunctionPrototype.next (native)
      at onFulfilled (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:64:19)
      at /Users/me/example/src/node_modules/koa/node_modules/co/index.js:53:5
      at Object.co (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:49:10)
      at Object.toPromise (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:117:63)
      at next (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:98:29)
      at onFulfilled (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:68:7)
      at /Users/me/example/src/node_modules/koa/node_modules/co/index.js:53:5
      at Object.co (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:49:10)
orokusaki
  • 55,146
  • 59
  • 179
  • 257

3 Answers3

20

To relocate middleware to another part of your app url-wise, you can use koa-mount.

'use strict';
const koa = require('koa');
const mount = require('koa-mount');

const app = koa();
app.use(function* () { this.body = 'Hello, world'; });
app.use(mount('/foo', function*() { this.body = 'Hello, foo'; }));

app.listen(3000);

curl localhost:3000
> 'Hello, world'
curl localhost:3000/foo
> 'Hello, foo'

koa-router itself does not support regex paths or parameter matching.

Dan
  • 10,282
  • 2
  • 37
  • 64
  • This isn't what I'm looking for. I'm talking about serving at a custom URL path from my own app, not from an external resource. IOW, I want to serve static files from my own, but from a `/static/` path (vs the root). – orokusaki Jul 12 '15 at 15:20
  • Sorry, I misunderstood. I answered before you edited your question, and have modified accordingly. – Dan Jul 12 '15 at 15:21
  • Thanks a lot. You've probably saved me 3 more miserable hours. I'm surprised the only way to achieve this (presumably) is by creating a separate app and mounting it this way (requires reregistering logging and other middleware), but it works perfectly. – orokusaki Jul 12 '15 at 15:29
  • `mount` works on any middleware (precisely, it works on a generator function) not necessarily a separate koa app (which just happens to also be a generator function). You could combine middleware using `koa-compose` and `mount` that instead. – Dan Jul 12 '15 at 15:30
  • 3
    I tried `app.use(mount('/static', serve(__dirname + '/static')));` (works perfectly) because koa-compose hasn't been updated in quite some time. I'll probably stick with a second app though, so that I can use many middleware. – orokusaki Jul 12 '15 at 15:39
  • you only need koa-compose if you are composing multiple middleware together (also, it hasn't been updated in a long time because it is a very very tiny module, there's not much *to* update) – Dan Jul 12 '15 at 15:40
13

Sure you can. And as the accepted answer says, the trick is using koa-mount along with koa-static. Though I don't understand why he doesn't provide an example with actual static files. This works for me (in Koa2):

.use(serve('public'))
.use(mount('/static', serve('static')))

Both folders (public and static) are in my project's root. But the first one is served when the user access to /, whereas the second one is served when they go to /static. That's all.

Lucio Mollinedo
  • 2,295
  • 1
  • 33
  • 28
1

you can use koa-static-folder. if you're still interested.

Kiril
  • 2,935
  • 1
  • 33
  • 41
Rei Dien
  • 196
  • 13