Koa.js - serving static files and REST API

前端 未结 3 2028
名媛妹妹
名媛妹妹 2021-02-09 02:50

I\'m new to koa.js library and I need some help. I\'m trying to make simple REST application using koa. I have a static html and javascript files I want to serve on route

3条回答
  •  孤城傲影
    2021-02-09 03:01

    From @Nurpax in the comments:

    app.use(async function (ctx, next) {
      return send(ctx, '/index.html', { root: paths.client()
    })
    .then(() => next()) }) 
    

    The key thing was to specify {root:}. I think the problem in my case was that for security reasons, send doesn't allow relative paths or paths outside of the project tree. Specifying the root param and then giving a filename relative to that seemed to fix the problem. I guess I expected koa-send to log an error/warning on the node output about this.

提交回复
热议问题