Skip to content

Node JS

Sandesh Kota edited this page Nov 27, 2019 · 56 revisions
  • A javascript runtime environment that executes the Javascript code outside of a browser. It can run on various platforms (Windows, Linux, Unix, Mac OS X, etc..)

Basic

  • Save the below code in a "firstNode.js" file
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello World!');
}).listen(8080);
d:\> node firstNode.js

Modules

  • Modules is a wrapper on set of functions. Similar to libraries. In the above example "http" is a module and by writing require('http') we will have access to HTTP module and its functionalities (like create server).

Custom Module

  • Add below code and create a file "firstmodule.js"
exports.myDateTime = function () {
  return Date();
};
  • Use it as below
var dt = require('./myfirstmodule');    -- file path
console.log(dt.myDateTime());

Clone this wiki locally