Getting started with Node JS

Erick Camacho
3 min readApr 27, 2018

--

What is Node js?

Node Js is an open source javascript runtime environment that executes javascript code on the server side. It is known for it’s speed compared to frameworks like Rails, Django and more. Node’s speed derives from it’s primary language Javascript which is event driven, this makes Node JS fast. One thing to know is that Node Js is not a framework but rather an environment, there are frameworks that work with Node such as Express and Sails which makes app building easy.

Node Js in it’s simplicity (src. openclassrooms.com)

Setting up a server using express

Let’s set up a simple server using Express to serve some static HTML

  1. First create a directory mkdir server-demo
  2. cd server-demo
  3. Initialize npm to create a package.json file which will hold the project dependecies npm init — yes
  4. Install express npm install express — save
  5. Create a server file touch server.js
  6. Create static html file touch index.html
  7. Navigate to your terminal and do node server.js
  8. Go to your browser and navigate to http://localhost:3000/
//server.js filevar express = require('express')
var app = express()
app.use(express.static(__dirname)) //serves the static html filevar server = app.listen(3000, () =>{console.log("server is listening on port", server.address().port);
}) //gets express server started and listening for requests

The above file starts the express server and serves static html file found in the same directory. The server is listening on port 3000.

<!-- index.html file --><!doctype html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="container">
<div class="jumbotron">
<h1 class="display-4"> Hello World!</h1>
</div>
</div>
Server running on port 3000
Static HTML being served from Express server

Routes

In express you can create the end points to perform CRUD functionality by creating routes. Below you can see we have created an array of objects with messages. These messages get served to a front-end via a created route http://localhost:3000/messages. This route can be called via an HTTP AJAX request from the client side using Javascript and grab the JSON data to display in the client side.

var express = require('express')
//npm install body-parser --save, used to parse through data and covert to json
var bodyParser = require('body-parser')
var app = express()
app.use(express.static(__dirname)) //serves the static html file
app.use(bodyParser.json()) //to json
var messages = [
{name: 'jerry', message:'Hi'},
{name: 'steve', message:'Hi'}
]
app.get('/messages',(req,res) => {
res.send(messages)
})
app.post('/messages',(req,res) => {
messages.push(req.body)
console.log("adding message", req.body)
console.log("a new message has been added",messages)
res.sendStatus(200)
})
var server = app.listen(3000, () =>{console.log("server is listening on port", server.address().port);
}) //gets express server started and listening for requests
Making a get request to our backend, serves us json data
Making a post request using postman receiving status 200 (ok)
Server shows us the message being added and the array of messages updated once it has happened

Summary

In this blog we have explored Node JS and it’s express framework. We have set up a server which serves HTML. From here the sky is the limit, we can add AJAX requests to the client to fetch from the backend. Databases can be linked to the backend such as Mongo DB, as well as web sockets with packages such as socket.io. Hopefully this blog can help you get started in the world of Node JS.

--

--