koajs / discussions

KoaJS Discussions
2 stars 2 forks source link

Either 404 or wrong content #39

Open chang-zhao opened 4 years ago

chang-zhao commented 4 years ago

Update: on Linux server it happens too.

I'm trying to figure out how to use koa-static (in SPA) properly.

First I just had put it in front of the other middleware, hoping that when it finds a file it would immediately return serving it:

const   Koa = require('koa')
    ,   app = new Koa()
    ,   static = require('koa-static')
app.use(static('css'))
app.use(static('img'))
app.use(static('js'))
app.use(my-other-middleware)
...
app.listen(32123)

But my-other-middleware rewrites the response (e.g. any css file has the same contents - of the main page). So I try to put an alternate route like this:

my-other-middleware = async (ctx, next) => {
    switch (ctx.url.substr(0, 4)) {
        case '/css':
        case '/js/':
        case '/img':
            return
            // or return next()
            // or next(); return — nothing helps
    }
...
}

So, for the "static" directories the response shouldn't get rewritten. But then I get 404 'Not Found' for any file which actually exist — e.g.

http://localhost:32123/css/a.css
http://localhost:32123/js/main.js
...

I don't even know how to debug it. Am I doing something wrong, or maybe it's just some incompatibility in my software stack? Bear with a noob question please.

Thank you.

chang-zhao commented 4 years ago

I tested it both on Windows 7 (through IntelliJ IDEA + Node.js etc.) and on Linux, the problem is the same.