如果我编写以下代码,我会得到“Hello World”,但不会得到“Hey from function”。我究竟做错了什么?如果我尝试从被调用的函数写入控制台,情况也是如此。好像没有被调用。
这是 Node.js 的非常基本的测试
const http = require('http');
const express = require('express');
const app = express();
const port = 3000;
function writetest(req, res) {
res.send("Hey from function");
}
app.get('/nodejs/', function(req, res) {
res.send('Hello World!');
writetest(req,res);
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
没有错误消息,只是似乎没有调用他的函数。
最佳答案
使用res.write()
,并且必须在最后调用res.end()
。
因为 res.send()
在内部调用 res.end()
并且您无法再写入 res
关于node.js - Node.js 中的基本函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56409666/