DEV Community

Cover image for Building first web server with NodeJS
Uddesh
Uddesh

Posted on

Building first web server with NodeJS

This is the first part of the series. In this series, we will start with the basics of NodeJS and move towards building a Restful API with all the features like Authentication, Database integration and much more.

Now the first question comes in mind is Why Node ?

NodeJS is a runtime environment of JavaScript so the developers already familiar with JavaScript can easily learn and use NodeJS.
Another benefit is Single threaded event loop that is responsible for abstracting I/O from external requests. There are many more reasons as well but these two are sufficient I think.

Now without wasting any time, we will directly jump to building an amazing restful API.

We will use express to build this API. If you have any queries regarding express go to their homepage and try to read the documentation.

Environment setup

  1. Download and Install NodeJS. Go for the LTS(long term support) version if you are confused.

  2. Download and install your favorite code editor. I prefer VSCode

  3. Create a project directory.

  4. Open terminal (windows user can use git bash), browse to project directory and write npm install express and hit enter.

  5. That's it for now.

Hello World in Node

As you are already familiar with JavaScript hello worlds. It will be the same for Node.

console.log('Hello World')
Enter fullscreen mode Exit fullscreen mode

First Web Server with Node and express

const express = require('express')
const app = express()

app.get('/', (req, res) => {
    res.send('Hello World')
})

const port = process.env.PORT || 3000
app.listen(port, () => console.log(`App is listning on port ${port}`))
Enter fullscreen mode Exit fullscreen mode

Explaination

In the very first line of the above code, we imported the express module which returns a function that we stored in a variable called express. In the second line, we called express function which returns an object and we stored it in a variable called app. After that, we have written the code to handle GET request. This get method takes two arguments.

  1. The path or the URL i.e. '/' (Root of the website)

  2. A callback function that will be called when we have an HTTP GET request. Now, this callback has two arguments req i.e. request and res i.e. response. The response is used to send the responses we want.

Now there are many different types of requests and we will talk about them in the upcoming posts.

In the last two lines, we are setting a port to listen to the requests.
We passed the port number and an optional callback function (it will log a message to the console) in the listen method.

Now to check things working properly

  • Run the program node

  • Open your favorite browser and goto localhost:3000. You will see a Hello World that indicates everything is working fine.

Finally Congrats you have successfully created a web server that can respond to your get requests.

Happy learning.

See you soon

Top comments (0)