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

HTTP Module

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {
  -- Add HTTP Header
  res.writeHead(200, {'Content-Type': 'text/html'});
  -- Read Query String
  res.write(req.url);
  -- Split Query String: http://localhost:8080/?year=2017&month=July
  var q = url.parse(req.url, true).query;
  var txt = q.year + " " + q.month;
  res.end(txt);
}).listen(8080);

File Server Module

  • Allows you to work with the file system on your computer (Read/Create/Update/Delete/Rename)
var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
  fs.readFile('demofile1.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });

  -- appends content if it doesn't exist. Else an empty file is created.
  fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
    if (err) throw err;
    console.log('Saved!');
  });

  -- Deleting a file 
  fs.unlink('mynewfile2.txt', function (err) {
    if (err) throw err;
    console.log('File deleted!');
  });

}).listen(8080);

URL Module

  • Breaking the URL and reading values from it
var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2017&month=february';
var q = url.parse(adr, true);

console.log(q.host); //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'
console.log(q.search); //returns '?year=2017&month=february'

var qdata = q.query; //returns an object: { year: 2017, month: 'february' }
console.log(qdata.month); //returns 'february'
var http = require('http');
var url = require('url');
var fs = require('fs');

http.createServer(function (req, res) {
  var q = url.parse(req.url, true);
  var filename = "." + q.pathname;
  fs.readFile(filename, function(err, data) {
    if (err) {
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    } 
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(8080);

Clone this wiki locally