-1

This is my code, there are 2 files:

file b.js

module.exports.data = function() {
    return new Date();
}

file a.js

var a = require("./b")
var http = require('http')
http.createServer(function(req, res) {
    res.writeHead(200, {'Content-type':'text/plain'})
    res.write('the date is: '+a.data)
    res.end();
}).listen(8000)

Why not print the date?

maazadeeb
  • 5,922
  • 2
  • 27
  • 40
Mark
  • 59
  • 10

3 Answers3

2

a.data is a function, may call it:

res.write('the date is: '+a.data());

Or you use a getter :

module.exports = {
  get date(){
     return new Date();
  }
 };

Then you can do:

res.write("date is "+a.date);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

you need to call the data function

var a = require("./b")
var http = require('http')
http.createServer(function(req, res){

    res.writeHead(200, {'Content-type':'text/plain'})

    res.write('the date is: '+a.data())

    res.end();



}).listen(8000)
Nirmal Dey
  • 191
  • 5
0

A simpler way of doing this will be: 1) In b.js:

module.exports={
 data:new Date()
}
Sarvesh Chitko
  • 158
  • 1
  • 9