DEV Community

loading...

Getting started with Node JS

poojagupta profile image Pooja Gupta ・2 min read

Hey folks! I just started learning node JS and thought to share with all of you who are new to node JS and wanted to learn node JS.
so we are gonna see little insights of node JS and after that we will write our first code in node JS.

So What is Node JS?

  • Node.js is a cross-platform JavaScript runtime environment that allows developers to build server-side and network applications with JavaScript.
  • NPM is a package manager which comes bundled with node JS which helps in downloading packages into a node_modules folder.
  • You can use npm init command to initialize your project which will ask basic information about your project and creates one file package.json which will have all information you provide.

  • So package.json file is face of your project which will contain all the information about your project as well as all those dependencies needed for your project with their version name.

  • Their are a lot of frameworks available which will ease our work while writing our code like express, sails, Hapi, Koa etc.

  • But first let we use simple Node JS without any framework to understand it in depth.

  • In your folder create index.js and do npm init after that write this code in index.js file and do node index.js from the command line from the same path where your index file exists.

const http = require("http");
const port = 8000;

function requestHandler(request, response) {
  console.log("Request received.");
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Welcome to node.js World");
  response.end();
}

const server = http.createServer(requestHandler);
server.listen(port, function(err){
    if(err){
        console.log(err);
        return;
    }

    console.log("Server is up and running:", port);
});
Enter fullscreen mode Exit fullscreen mode

In this case, on each and every url, you found “welcome to node.js world”. Now we will send different response, according to requested url.

const http = require("http");
const port = 8000;

function requestHandler(req, res) {
    if (req.url == "/") {
        res.writeHead(200, { "Content-Type": "text/html" });
        res.end("Welcome to the homepage!");
    }

    // About page
    else if (req.url == "/contact") {
        res.writeHead(200, { "Content-Type": "text/html" });
        res.end("Welcome to the contact page!");
    }

    // 404'd!
    else {
        res.writeHead(404, { "Content-Type": "text/plain" });
        res.end("404 error! File not found.");
    }
}

const server = http.createServer(requestHandler);
server.listen(port, function(err){
    if(err){
        console.log(err);
        return;
    }

    console.log("Server is up and running:", port);
});
console.log("Server has started.");
Enter fullscreen mode Exit fullscreen mode

Here, we track requested url and respectively show that content on that url, and handle 404 condition in all other cases.
I hope this article helps you getting started with node JS, don't forget to tell me in comment section how much you liked this post and in case any query reach me out !
Happy Coding!

Discussion (0)

Forem Open with the Forem app