代码如下(处理静态文件):
if (requestPath.startsWith(staticsBasePath)) {
let absolutePath = path.join(dir, requestPath);//文件的路径
fs.stat(absolutePath, (error, stats) => {//判断文件是否存在
if (error || !stats.isFile()) {
ctx.response.status = 404;
} else if (stats.isFile()) {//判断是否是一个文件
fs.readFile(absolutePath, (error, data) => {
if (error) {
ctx.response.status = 404;
}
ctx.response.type = mime.lookup(requestPath);//这里报错了
ctx.response.body = data;
})
}
});
} else {
await next();
}
下边这句报错Can't set headers after they are sent.
ctx.response.type
为什么回报这个错呢?
因为你之前已经response出去了