关于中间件static返回固定内容的问题
发布于 7 年前 作者 Ireoo 3193 次浏览 来自 问答
app.use(express.static(__dirname + '/public/theme', {setHeaders: stop}));

function stop(res, path, stat) {
    console.log(res.req.headers);
    if(!/192.168.1.106/.test(res.req.headers.referer)) {
        res.statusCode = 404;
        res.setHeader('Content-Type', 'text/plain');
        res.send('Cannot ' + req.method + ' ' + req.url);
    }
}

res.send();怎么破。。。。

_http_outgoing.js:359
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at ServerResponse.setHeader (_http_outgoing.js:359:11)
    at SendStream.setHeader (*\node_modules\send\index.js:815:9)
    at SendStream.send (*\node_modules\send\index.js:579:8)
    at onstat (*\node_modules\send\index.js:677:10)
    at FSReqWrap.oncomplete (fs.js:111:15)
1 回复

返回消息的时候 return 破

回到顶部