Slip 10
Write node js script to build Your Own Node.js Module. Use require (‘http’) module is a built in
Node module that invokes the functionality of the HTTP library to create a local server. Also use the
export statement to make functions in your module available externally. Create a new text file to
contain the functions in your module called, “modules.js” and add this function to return today’s date
and time.
modules.js
// datetime_app.js
function datetime() {
let dt = new Date();
let date = ("0" + dt.getDate()).slice(-2);
let month = ("0" + (dt.getMonth() + 1)).slice(-2);
let year = dt.getFullYear();
let hours = ("0" + dt.getHours()).slice(-2);
let minutes = ("0" + dt.getMinutes()).slice(-2);
let seconds = ("0" + dt.getSeconds()).slice(-2);
let output = year + "-" + month + "-" + date +
" " + hours + ":" + minutes + ":" + seconds;
return output;
}
module.exports = {datetime}
slip10.js
var http = require('http');
var dt = require('./modules');
var server = http.createServer(function(req,res){
res.writeHead(200,{'content-type':'text/html'});
const result = dt.datetime();
res.write('current date and time is ');
res.write(result);
res.end();
});
server.listen(1234);
Comments
Post a Comment