Skip to content
Cover image for "Learning NodeJS: Creating Your Own Web Server with NodeJS" - Blog post by Muhammad Rivki about Backend

Learning NodeJS: Creating Your Own Web Server with NodeJS

Written on 3 min read
Available in:
🇺🇸EN🇮🇩ID

If you've ever created a static HTML website or used PHP or ASP before, you've likely worked with a web server like Apache or IIS that serves static files requested by the browser.

For example, if you create about.html and place it in the correct folder (example: www/), you can access that page at the URL http://localhost/about.html. Depending on your web server configuration, you can omit the ".html" extension.

Node provides a different approach from traditional web servers. In Node itself, the application you create is the web server itself. But creating a web server in Node is simple and not too complicated. If you haven't installed Node on your device yet, follow this article.

Hello World NodeJS

As usual, when we want to learn a new language, we create a "Hello World" application. Let's get our hands dirty.

In your favorite code editor, create a helloworld.js file and type the code like below. Try to type it yourself to engage your motor skills and brain. Don't copy-paste while following a tutorial.

const http = require('http')
const port = 3000

const server = http.createServer((request, response) => {
  response.writeHead(200, { 'Content-Type': 'text/plain' })
  response.end('Hello world!')
})

server.listen(port, () => {
  console.log(`server started on port ${port}`)
  console.log('press Ctrl-C to terminate....')
})

Okay, let me explain what the code above does line by line.

  • On line 1, we call the built-in "http" module from NodeJS and assign it to the variable http. This module is used for HTTP server and client operations.
  • On line 2, we declare a port variable with the value 3000
  • On lines 4-6, we create our own HTTP Server and assign it to the server variable. The createServer method is built into the http module. If you want to know what else is in the http module, you can read the HTTP documentation. The argument required by the createServer method is a function with request and response parameters.
  • On line 5, we create a response header with status code 200 and Content-Type: text/plain. You can see headers like this in the browser's network request.
Network Tab
Network Tab
  • On line 6, we call the response.end method. This method completes the client's request and displays Hello world! in the browser. This method should be placed at the end of a function.
  • On lines 9-12, we call the listen method on the server variable. Its job is to start the server. This method requires 2 arguments like this (port: Number, callback: Function). In the callback function, we simply add a log.

If you're using VSCode, you can use the shortcut cmd | ctrl + ` to open a terminal in VSCode. If not using VSCode, make sure your terminal is in the correct folder. After that, we can run the file by typing the following command:

node helloworld.js

It will appear like this in your terminal/command line:

Start Server

Then you can access the web page at http://localhost:3000/

Result
Result

Summary

So this time we've learned how to create a web server ourselves using only NodeJS's built-in 'http' module. In the future, we'll expand our knowledge and not stop here. Next, I'll add routing using only NodeJS. So you need to strengthen your foundation by learning the truly basics first. We don't jump directly into using a framework.

Credits

Blog