Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

node.js - How can I serve a static file using Koa?

I want to implement universal links in my project and I need to serve a json via Koa, which is a static file named apple-app-site-association.

My file is located in /assets/apple-app-site-association/apple-app-site-association folder.

My concern is that I cannot access this https://myprojectlink/apple-app-site-association.

What I have at this moment:

const path = require("path");
const Koa = require("koa");
const mount = require("koa-mount");
const serve = require("koa-better-serve");

app.use(mount("/apple-app-site-association", serve(path.resolve(__dirname,"../../../assets/apple-app-site-association/apple-app-site-association"))));

I get Not Found, it seems like I cannot serve it in the right way.

What can I do?

Thank you very much in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

serve (koa-better-serve), like most static server middlewares for Node frameworks, takes a path to a directory, not a path to a single file. You can also get rid of the mount() call, koa-mount is for mounting other Koa apps as middleware.

app.use(serve(path.resolve(__dirname,"../../../assets/apple-app-site-association/")));


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...