我在网上查找并找到了解决方案,但无法正常工作。我不知道为什么。这是我的代码;
app.get('/', (req, res) => {
fs.readFile('index.html', function (err, data) {
res.write(data);
res.end('');
})
});
这一工作按预期进行。但是如果我将通配符路由添加为
app.get('/:randomEndPoint', (req, res) => {
console.log(req.params.randomEndPoint);
fs.readFile('index.html', function (err, data) {
res.write(data);
res.end('');
})
});
我可以在控制台上正确记录端点。但是,res.write命令不起作用。这是错误消息;
_http_outgoing.js:595 throw new ERR_INVALID_ARG_TYPE('first argument', ^
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must
be one of type string or Buffer. Received type undefined
at write_ (_http_outgoing.js:595:11) at ServerResponse.write
(_http_outgoing.js:567:10)
谢谢。
即使对我来说没有任何意义,使用readFileSync也可以。
const index = fs.readFileSync('./dist/index.html', 'utf8');
app.get('/*', (req, res) => {
res.write(index);
res.end('');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句