📅  最后修改于: 2022-03-11 14:59:05.508000             🧑  作者: Mango
const Koa = require('koa');
const app = new Koa();
const mime = require('mime-types')
const fs = require('fs');
app.use(ctx => {
if (ctx.request.url === '/file') {
var path = "/Users/apple/Downloads/test.zip";
var mimeType = mime.lookup(path);
const src = fs.createReadStream(path);
ctx.response.set("content-type", mimeType);
ctx.response.set("content-disposition", "attachment; filename=test.zip");
ctx.body = src;
} else if (ctx.request.url === '/image') {
var path = "/Users/apple/Downloads/201228bli.bmp";
var mimeType = mime.lookup(path);
const src = fs.createReadStream(path);
ctx.response.set("content-type", mimeType);
ctx.body = src;
} else if (ctx.request.url === '/json') {
var jsonType = mime.lookup('json');
ctx.response.set("content-type", jsonType);
var json = {text: "Hello, World!"};
ctx.body = JSON.stringify(json);
} else if (ctx.request.url === '/xml') {
var jsonType = mime.lookup('xml');
ctx.response.set("content-type", jsonType);
var html = `
hello
`;
ctx.body = html;
} else {
ctx.body = "Hello World";
}
});
app.listen(8000);