Beginner’s guide to build your first Node.js server

Sagun Jaiswal
4 min readOct 4, 2020

--

Getting started with Node.js can be pretty overwhelming, so, let me help you a bit and let you build your first server!

Lets quickly go through what is Node.js?

Node.js is an open-source server-side runtime environment built on Chrome’s V8 JavaScript engine. Sounds great but how does that work?

JavaScript was previously used only in the frontend but with the help of Node.js, we can use it on the server-side as well. JavaScript can run in browsers due to the V8 engine (which is written in C++).

You want to compile JavaScript outside the browsers? 🧐

That is when Node.js comes into play! Node.js wraps the V8 engine so that it can run it in our machine as a standalone application.

Node.js Architecture

Node.js makes it easy to create a simple web server that processes incoming requests asynchronously. Well, now that we know what Node.js does, let's create a simple Node.js web server.

Make sure Node.js is installed on your computer. Following is a simple example of a Node.js server contained in a file named say server.js

Initial file structure of node.js app

Node.js has a built-in module called HTTP, which allows Node. js to transfer data over the HyperText Transfer Protocol (HTTP). The createServer() method creates a server on your computer. To run the file, enter the following command in your system terminal

node <ENTRYPOINT_FILENAME>
eg:
node server

in the terminal and press enter. Now if you go to http://localhost:8000 on your machine . You would see something like this :

Now come back to your system terminal you would be able to see the log there and not in the browser’s console!

logging in system console

And congratulations you have successfully made your first simple Node.js server.

Now, let's try to improve and understand things a bit more.

We did it! Did we?

Now let’s explore the request(req) and response(res) object a bit in detail.

using response and request

here, we have set the content-type to be plain text in the next example we will set it to HTML so that its more fun!

TIP: Whenever you change the code, don't forget to restart the server so type in again in your terminal. (you can also use nodemon to get rid of the hassle continuously restarting the server whenever you make changes to your piece of art)

node <ENTRYPOINT_FILENAME>
eg:
node server.js
Showing currently made changes in the browser

and come back to your terminal you will see the whole request object.

RETURNING HTML PAGES

Lets now see a more practical example of returning a HTML page .

using node file handling(fs) module

fs is the Node.js file system module that allows you to work with the file system on your computer. The fs.readFile() method is used to read files on your computer.

Now when you go to http://localhost:8000/ you will be able to see the contents of your HTML page.

That’s it we have created our first Node.js server successfully.

Thanks for reading till the end! This is my first blog, I would be more than happy to get your feedback and suggestions. Find me here :

LinkedIn: https://www.linkedin.com/in/sagun-jaiswal/

GitHub: https://github.com/sagunjaiswal

--

--