Create a Node server without express

Create a Node server without express

Hello guys, today I'll be showing you how to setup a simple server using node js and next article, I'll cover synchronizing your node server to use express js. So let's get started...

Requirements

  • Node
  • Npm
  • Nodemon

For us to create our basic server, we need to have node installed on our machine and for us to do this, visit Node JS and download that which is suitable for your machine, be it linux, windows and all.

Once you have node installed, it comes with npm package which we'll be using from time to time to get other packages we need on our application.

To confirm that node and npm is installed on your machine properly, type this command on your terminal to see the version based on what you installed:

node -v

You'll get a response of which version you have installed. Do the same for npm in this way:

npm -v

Once everything is confirmed, we're done getting node installed in our machine. So let's create our server.

Open up your terminal and make a simple folder:

mkdir node-server

You can change your folder name to what ever you like. After sending the command above, cd into it

cd node-server

Once you are in the folder, type or send

npm init

Follow through the proces by hitting your enter button down to the last section where at the end you'll have a package.json file generated for you.

Alright cool, let's create our sever now. Open the folder in your favourite code editor and create a file called index.js. In that file, enter the following command:

const http = require("http")
const server = http.createServer((req, res) =>{
    res.end("Server is running")
});

server.listen(process.env.PORT || 3000)

What does the above code do? Because we are creating a server, we need to connect via http that was why we imported http.

The second line shows where we used our http imported to create a server passing in the request and response params to it. Normally a request and response is gotten and we utilized the end response on the other line to send to the client side of our app that our server is running.

Finally we need to listen to a port. The process.env.PORT is just to allow our app listen to any port whenever it is deployed while the 3000 is whenever we're working locally.

How do we run our server🤔? Well remember we have node installed, so we can simply open our terminal from our code editor and run

node index.js

Viola!!! Our server is running. Open any browser and visit localhost:3000 to see the response from our server. But there is one thing, whenever we make a change on our app, we have to restart our server all over by killing the terminal. And to prevent this, we need to install a package called nodemon, so let's do that by entering into our app's folder and installing with this command via our terminal

npm i -g nodemon

Once installed, you can start your server now with this new command:

nodemon index.js

Your server should be up and running and whenever you make a change, it should update automatically without restarting your server.

Happy hacking!!