node js application code example

Example 1: nodejs hello world

const http = require('http');

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

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

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

Example 2: create node project

#1. server.js
var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.status(200).send('Hello World!');
});

var port = process.env.PORT || 3000;

var server = app.listen(port, function() {
  console.log('Express server listening on port ' + port);
});

open cmd run server.js
"node server.js"
log:Express server listening on port 3000

& then

open link "http://localhost:3000/" in your browser and show result.

Example 3: node js

NodeJs is Runtime environment for executing Js code, Outside Of Browser.
it is build upon Chrome v8 engine using c++.

Uses of Node:

-can build back-end services like API which can be used as backend for 
diferent platform Apps(Website, MobileApp, Desktop App).

-is Asynchronous by default means Single thread handle all the request
and response, thereby providing more speed, highly scalable, built time half 
compare to other tech in market, 33% few line of code, 40% fewer files,
can handle 2times more requests/secs, 35% fast response time.
// Hope you like the short Description.