And that's why frameworks like ExpressJS, HapiJS exist, which makes creating servers in NodeJS very easy. With an increasing number of routes, such things become hard to manage as we will have to add multiple if statements. We saw how we had to handle each request with its method and the URL. With that, we have created a simple node server to serve static content as well a simple API to return JSON data.
Now we will create a node server that will serve our index.html file when a request is sent to /. Index.html Demo Coming from the Root Step 3: Add our server code to serve the static assets Create a directory called public at the root of your project.Īdd a file named index.html with the following content.
Create a file called server.js in your directory.By this time, you will have a package.json file in your directory.Step 1: Initialize the projectĬreate a folder by any name and initialize a node project in it using npm init -y In the process, you will also learn about the request and response objects provided by NodeJS, which are used by every other framework built on top of NodeJS like ExpressJS etc. We will create a simple server that will serve static assets and a simple API to return some JSON data. In this blog, I will show you how to create a NodeJS server using core http module.