03.Node – Start With Example.

Node – Start With Example.




Steps,

  1. Make a folder – mkdir   folderName  (my folder name is nodeExample)
  2. Go to indide the folder – cd nodeExample
  3. Create a javaScript file inside nodeExample folder – example.js
  4. Copy and paste bellow code and save.
  5. Run js file (server).  D:\OTHER\nodeExample\node example.js


1st Example

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((reqres=> {
  //res.writeHead(200,{'Content-Type' : 'text/plain'});
  res.statusCode = 200;
  res.setHeader('Content-Type''text/plain');
  res.end('Hello World\n');
});

server.listen(porthostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});


Node Module are the way to import already build code I node.js.(like java package or class,library)

const http = require('http');

In here, require is similar to import in java, include in c++.

Anonymous Function, Billow both are same.

var func = function(req,res){}
http.createServer(func);

http.createServer(function(req,res){});


.

Comments

Popular posts from this blog

09.Data Binding.

Database - Topics

02. Spring – Creating spring project clone it with GIT step by step.