Creating a node.js http/express server

Hey  there!! In this blog I am going to walk through step by step how to create a node.js http/express server, with a simple breakdown of what each part does.

 

What in the world is node.js?

Node.js is an open source platform that allows developer to create a Web server, basically something for your application to run on.Node.js runs on the Javascript runtime environment but is not a Javascript framework.Node.js has a collection of “modules”that help you tackle task like for an example networking.  If you you are unfamiliar on the purpose of a web server well let me tell you. A web server uses something called http (Hypertext Transfer Protocol)which overall helps web applications and web pages connect to the user taking in request from the user dealing with the application and the response to the user from the app. This process is what people may refer as the client/server model, the client (you) sends a tweet which is an request and the server directs that tweet to the most recent post response. The node.js modules are very low- level doing very complex things, that’s why modules come in handy you can do a lot with little code. Me personally anytime i’m creating a web app I prefer to run it on a local server like node.js. In my experience it has always been a faster load time when dealing with files like images and code, its just less of a headache to deal with to avoid browser and load errors.

 

Creating a basic http server

What you need to do first is to create a project folder, then inside your folder create a file called “server.js”. Go into your text editor and open that project folder up  in you editor.

Now to get started in your “server.js” file lets call the ‘http’ module thats automatically embedded into node.js and store it in a variable called http by typing this:

var http = require('http');

 

Next we are going to create a server with the method “createServer();”.Now the createServer method in its parameter the method takes in a “requestHandler” like so.

 var server = http.createServer(requestHandler);

 

Take the requestHandler that we past through in the createServer method and this is going to be our callback method anytime  user wants to connect to our server.In the method we will log to the console reqeust.url which gathers infomation about the request we sent.lastly responing to the user in the console with a message like “server running..”.Also in the begining passing through the request and respone arguements.

var requestHandler = (request, respone) => {
 console.log(request.url);
 respone.end('Server running....');
}

 

Now for our server to be able to connect the user to the content it needs a place  to direct the user to whatever they are trying to connect too. What we need is to tell the server what ‘port’ we are establishing the application on. First, we are going to write a variable called port and store 3000 in it.

var port = 3000;

 

Next we are going to take the “server” variable and allow for our server we created to ‘listen’ for port 3000 and pass in the err method.Now ‘err’ is a method to allow us to take in error handling for insteads, if the port we called is areadly taken then our server will not run. If not then run as planned and log out a message telling the user where to go.

 server.listen(port, (err) => {
  if (err) {
   return console.log('Server error',err);
  }

  console.log(`server is listening on localhost:${port}`)
 })

 

Reminder that localhost is a variable that stores your IP (Internet Protocol)address then its the port number that your ip address is directed too.

var http = require('http');
var server = http.createServer(requestHandler);

var requestHandler = (request, respone) => {
 console.log(request.url);
 respone.end('Server running....');
}


var port = 3000;server.listen(port, (err) => {
 
if (err) {
 return console.log('Server error',err);
 }

 console.log(`server is listening on localhost:${port}`)
})

 

Lastly, in the same folder create an html file and make an h1 tag with message. Run the server within that ‘http-server’ folder and point your browser to localhost:3000.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s