Setting up web Server — Node.js

Photo by Emile Perron on Unsplash

As a frontend Dev, mostly I look at the frontend technologies which as Reactjs, hbs, ejs (templating engines), HTML, CSS, and so on. off late I have my eyes on the backend Node.js.

I hope this will help someone to kickstart as the backend dev.

Since I am fond of the JS community, I would like to use Node.js as my backend technology to start a webserver. I will be using the express framework to do so.

Project Setup

Initializing and setting up the project can be done with a single command.

The above command comprises of the three commands,

  1. — Abbreviated to make the directory, we are creating the directory.
    The command takes a parameter which is the name of the project.
  2. Abbreviated to Change Directory, we will change to the directory that is being created with this command.
    The command also takes a parameter which is the name of the project.
  3. — npm stands to node package manager & init stands to initializing the repository.
    — will create & output the code like below

Now accordingly the repository is created and we can start installing the dependency based on our needs.

Introduction to the Express Framework.

Express is a framework for Node.js, which actually makes it easy to build the webserver. Also, we can use the native HTTP module to build the webserver too but express helps us in setting all the constructs very easily.

Installing the dependency

The above command installs the express package into our dependency and also we can pass to save it as a dev dependency as to dependency to the whole project or folder.

Exploring the Express with the Project

I will create a simple file called In which we can start initializing the framework or library and then initialize the application with a port.

Let me start explaining the content of the file

First-line indicates we are just initializing the express variable with the package installed in our code

Most of us will be aware of the commonjs module system, where is a function that allows us to import or include the modules of the separate file or packages.

The above line indicates that the express function “express()” and puts new Express application inside the app variable (to start a new Express application). It’s something like you are creating an object of a class. Where “express()” is just like class and app is its newly created object.

we are just defining the port so that the port can be reutilized.

Listen is a function of the express instance, where it takes port as a parameter, and then the call back here we have specified is to notify that the application is running successfully in the denied port.

Running of the simple Express Web Server

We can do it in two methods,

  1. starting up with the file itself,
    What this above command does is it will run the file using the node (which will be installed globally)
    Node installation.
  2. using the file.
    we can simply write a script inside the file to start running the Node.js application.

This will help in setting up the web server in the easiest yet effective way using Node.js & express.

The complete code base is also been shared below.

https://gist.github.com/pramodmg/896371ee17fb6221c53ec5c9d93d8028

Conclusion

Thanks for reading! I hope this article was helpful for you to get started with Express. In case of any queries or doubts reach out to

Please continue to support the blog.
Would be exploring more in terms of the express in the coming blogging where I will be covering the routing, middleware, body parsers, CRUD operations, and few more topics.

Twitter: https://twitter.com/pramod_pummy
LinkedIn: https://www.linkedin.com/in/pramodmg/

Last Blog on CORS: Don't forget to check it out, will leave the link below,

--

--

Tech Stuff

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store