Introduction to Nodejs | 100 seconds paper

A brief introduction with examples.

·

3 min read

Introduction to Nodejs | 100 seconds paper

What is Nodejs?

Node js, an asynchronous event-driven javascript runtime designed for building scalable network applications. Designed by Ryan Dahl and released in 2009, Nodejs is a javascript runtime built on google's open-source v8 js engine second line.

Js is run in a browser so the browser is js runtime, Nodejs enables the environment running js outside the browser on top of a v8 engine, like a container so it is js runtime. nodejs.png

Uses of Nodejs

Nodejs is used for building fast and scalable data-intensive applications. Server-side apps, API with DB, data streaming, server-side web apps, and real-time chat apps all can be made with node js.

Architecture of Nodejs

Following single thread architecture, node js follows a non-blocking event-driven coding pattern to ensure the process run for every use without blocking. thread.png

Simple server in Nodejs

Example: Import HTTP module. Now use the createServer method on it and send a response from the callback. The callback function has a request and response parameter. Then save on a variable and listen using that variable on desired port and IP.

const http = require('http')
const server = http.createServer((req,res)=>{
    res.end("Hell from server of roshan")
})
server.listen(8000, '127.0.0.1',()=>{
    console.log("listening on port 8000")
})

Routing in Nodejs

Example of routing on nodejs: Import HTTP module and save it on a variable, then use createServer method on HTTP with callback function with req and res as a variable, save URL from request in a variable use if else to send the response as per pathname you can write a custom header as per the status code then listen to your desired port and IP.

const http = require('http')
const server = http.createServer((req,res)=>{
    const pathName = req.url
    if(pathName === "/" || pathName === "/overview")
    {
        res.end("Hell from server overview")
    }
    else if(pathName === "/products")
    {
        res.writeHead(200, {
            'content-type': 'text/html'
        })
        res.end("<h1>hello from product page</h1>")
    }
    else{
        res.writeHead(404, {
            'content-type': "text/html",
            'my-own':'hehe' 
        })
        res.end("<h1>404 error: Page not found</h1>")
    }
})
server.listen(8000, '127.0.0.1',()=>{
    console.log("listening on port 8000")
})

Simple API in Nodejs

To import content from a JSON file we need to import the fs module, then http now read a file with a pathname and utf-8 encoding then parse data into JSON and save on variable create server and route as per request URL for the response also, you can specify header content. Then listen for the request.

const fs = require('fs')
const http = require('http')

const data = fs.readFileSync(${__dirname}/data/data.json, 'utf-8')
const productData = JSON.parse(data)

const server = http.createServer((req, res)=>{
    const path = req.url
    if(path === '/api')
    {
        res.writeHead(200,{
            'content-type': 'application/json'
        })
        res.end(data)
    }
    else if(path === "/")
    {
        res.end("server is running")
    }
    else{
        res.writeHead(404,
            {
                'content-type': 'text/html'
            })
            res.end("<h1>404 error: page not found</h1>")
    }
})
server.listen(8000, '127.0.0.1', ()=>
{
    console.log("listening on port 8000")
})

Does anyone use Nodejs?

Nodejs has many frameworks and libraries as per your need. Expressjs, socket.io, next.js, meteorjs are some of them. Big community, millions of packages and modules, and used big corporations like Netflix, eBay, PayPal, and uber, Nodejs popularity is increasing daily and so does job opportunity.