Understand the basics of Node and Express


Image result for node js and express


I am pretty sure most of you might have heard about the MEAN stack if you are a web developer for quite some time now. For those of you who don't know about MEAN stack, it is basically a set of technologies(or frameworks to be specific) for building Full Stack Web Applications. MEAN stands for MongoDB, Express, Angular and Node. Let me brief very shortly about these technologies. MongoDB is a NoSQL Database(Say Hello to NoSQL Databases) which is used to store and process the data in your application. Express is a Node.js framework, Angular is a Javascript framework to build the frontend of your application and Node.js is a Javascript runtime which makes it possible to use Javascript on the server-side.

This article is aimed for beginner developers and anyone interested in getting up and running with Node.js. Before diving into this article, you should be confident enough with JavaScript to know the basic concepts of the language. Technical terms regarding Node will be explained and linked below.

As this article aims at Node and Express, you must first understand what these technologies are and why they have taken over a large section of the back-end of many web applications these days:

What is Node.js ?

Node is an asynchronous event driven JavaScript runtime built upon Chrome’s V8 JavaScript engine. It’s designed to build scalable network applications. Now that was a very clear and bookish kind of definition. In layman's terms, it basically allows you to write Javascript on the server side.You might be wondering how as Javascript was designed to work on the browser but that was late 90s. Its been a lot of progress amongst JS Standards these days. 

Basically, the creator of Node.js took the Chrome's V8 Engine and built a runtime to work with JS on server side.By runtime, I just mean an environment where the language can get interpreted. So basically now we have a way to write Javascript on the backend.Now lets focus on the word Asynchronous in the main definition. By default, Javascript is single threaded(A thread is simply an independent path of execution).(Learn more about Threads). So there is only one thread, the Main thread. Asynchronous simply means "execution without interrupting the main thread". In this way, the user gets a non-blocking execution, which makes Node one of the fastest tools for using in Web Applications today.  So that was a lot of theoretical part to make your basics strong. Now let's drive straight into Node and write our first Node and Express app.

1. Install Node for your specific platform




It is very easy to install Node. Just head to nodejs.org and follow the simple installation process for your respective operating system. Now head over to any directory on your system to create your first Node and Express server. Create a simple directory here and navigate into it.





Next run the following on the command line:





Just enter the default values everywhere and hit enter. Type yes in the end and you are all set.

You should have a directory structure like this now -


Now we need to install express via NPM(Node Package Manager). Now NPM is basically a package manager for Node.js that comes pre installed when you install Node on your system.You can install all modules and dependencies(technologies needed in your project in Layman's language) via the NPM.
To install Express via NPM, just enter the following command and hit enter.



Now we are all set to code our Hello World application using Node and Express. Create a index.js file in the same directory and enter the following code into it :

Now lets break down the code that we just wrote :
In the very first line, we are importing the express framework using require() method, provided by Node. This tell us that we want to use Express in out app.

Next, we are intialising our app by invoking the function that the express framework just provided to us in the above line. We have now initialised our app and we are ready to Express now.

In the next line,we are making a GET request( I will soon be covering HTTP requests in my upcoming blog posts) to the Home Route('/'). This means that whenever user visits the localhost/ on our computer, we need to fire a callback function, which will get executed whenever user visits localhost/ on our computer. The callback function takes 2 parameters, the request object and the response object. In the next line, we are using the response object to send some data back to the browser and that data is "Hello World". 

Finally, in the subsequent lines, we are listening to our server on a particular network port on our computer(3000 in our case) which again fires a callback function, once our server starts running and all that callback does is log a message onto the console which says that the server was successfully started on port 3000.

Just save your file now and run it using the following command:




If everything worked fine, you should see this message in your command prompt and now open your browser and type the URL : localhost:3000.

You should now see Hello World in your browser and Congratulations, you have now written your very first Express app.


I hope this post was useful for you all and gave you a clear understanding of Express and Node and you also understood how a basic Hello World app works in express. Theres a lot more to explore in Express and Node but I hope this cleared the basics of Express and Node and also why they are used. Thank you for reading this blog post and stay connected for more content. Till then, keep coding, keep developing and have a nice day!

Comments

  1. Hi Rahul, there is a missing part of your post, after the phrase: "Create a index.js file in the same directory and enter the following code into it :".

    Great stuff in your blog.
    Thanks for sharing.

    ReplyDelete
    Replies
    1. Sorry dude actually it seems to be some issue with the Github gist I used for code in my post. It's back now.

      Delete

Post a Comment