-1

So I created a module for node js and connected it to the main file, then ran the server in localhost:8080. The code ran but, this happened-

display of localhost:8080

this is the code for the main node.js file-

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;)

The code for module-

exports.myDateTime = function () {
return Date();
};
3
  • "display of localhost:8080" is the link to the screenshot of what happened Commented May 16, 2021 at 18:31
  • res.write("Date and time:" + dt.myDateTime) -> you need to actually call your function: dt.myDateTime() Commented May 16, 2021 at 18:32
  • You didn't call the function Commented May 16, 2021 at 18:32

1 Answer 1

1

This is a function, you need to call it otherwise it will just print the function

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

Change to this:

res.write("Date and time:" + dt.myDateTime());
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.