【问题标题】:node js (javascript) code doesn't call function but instead shows the code as text [duplicate]node js(javascript)代码不调用函数,而是将代码显示为文本[重复]
【发布时间】:2021-05-16 18:30:41
【问题描述】:

所以我为node js创建了一个模块并将其连接到主文件,然后在localhost:8080中运行服务器。代码运行了,但是发生了这种情况-

display of localhost:8080

这是主 node.js 文件的代码-

var http = require('http');
var dt = require('./demo_module.js');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write("Date and time:" + dt.myDateTime)
  res.end('Hello World!');
}}.listen(8080;)

模块代码-

exports.myDateTime = function () {
return Date();
};

【问题讨论】:

  • "display of localhost:8080" 是所发生的截图的链接
  • res.write("Date and time:" + dt.myDateTime) -> 你需要实际调用你的函数:dt.myDateTime()
  • 你没有调用函数

标签: javascript node.js


【解决方案1】:

这是一个函数,你需要调用它,否则它只会打印这个函数

exports.myDateTime = function () {
  return Date();
};

改成这样:

res.write("Date and time:" + dt.myDateTime());

【讨论】:

  • 感谢您的帮助。现在可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多