Using node.js and express framework

Hi so I’ve been away and haven’t been on the laptop or desktop for a week, which felt like a long time but also very nice! I have a lot of stuff to catch up on and have decided to write this post on using node.js and express. I’m following a video series by ‘LearnCode.academy’ which my friend sent me on node.js. This video series covers topics such as routes and APIs, oAuth, setting up a server etc.

As I have mentioned before, this blog is to go over what I’ve learnt so this post will focus on setting up an express server. The structure of this basic program will be as follows in the image below:

Screenshot from 2017-05-28 14-51-12

The server.js file is for setting up our server and the users.js contains the information we need that deals with the routes we are using. We also need to require express in this file and we also setup a router by the line:

const router = express.Router();

In this users.js file we declare our JSON user object and set the url parameters that will be used to select different elements within the user object. The code will be included later on in this post.

The package.json file is created when we run the command:

npm init -y

This file includes all the details and metadata of the project, including dependencies and by including the -y flag all fields are inputted with the default values. Every time we install a node package by running:

npm install packagename

The package is intalled the working directory, under ‘node_modules’. In order to save the package within our dependencies in the package.json file, we need to use one of two flags:

. --save-dev
. --save

The --save-dev command should be used when you want to use a package for developers, for example if you want to be able to use grunt or gulp. However, these dependencies won’t be available when the code goes into production.

The –save command is used when you want to install a package that will be used in production. It is also worth noting that you can install a package by running:

npm install packagename

However this is not recommended as it will not be installed in the dependencies section of the package.json file and other developers who use your module won’t be able to determine the dependencies of the package.

Express is a web framework that is used with node.js and is extremely popular, it is written in JavaScript. Before continuing, it is worth going through a bit about web servers.

A web server can refer to hardware, software or indeed both of them working together. For example, the hardware of a web server stores the websites component files such as CSS stylesheets and JavaScript files – these then get transferred to the end-users device. This server is connected to the internet and can be accessed through a domain name.

An example of the software a web server uses is an HTTP server. A HTTP server sends and receives requests and understands URL (website addresseses). The most basic chain of events is that a browser requests some information via a HTTP request from the server, the server then responds with a response via a HTTP response. If you want to read more about this, click here.

webserver-basic-sm

There are two different types of servers:

  • static: the server sends files ‘as is’ to the browser, without modification of these files.
  • dynamic: most commonly a static web server which uses extra software, usually an application server and a database. The application server usually updates the hosted files before sending them back to the browser via the HTTP server.

I am fairly new to the web and so feel this is a steep learning curve and there is so much to learn about how the web works, as I am from quite a traditional JAVA background, having done a part time evening university course in JAVA and have not focused much on the web. However, I am very interested in security and think that there are loads of opportunities in JavaScript and the web so have decided for the moment to focus more on this.

To begin with this simple program, I had to set up an express server, this is done by the following code:

const express = require (‘express’);

const app = express();

app.listen(3000);

In this example, we also need to npm install express so that we are able to ‘require’ it. We are creating an instance of express and telling it to listen on port 3000.

Therefore we will be accessing the application when visiting:

localhost:3000/

Below is an example of the server.js file:


const express = require("express");
const app = express();
const users = require("./routes/users");

app.use(users);
app.listen(3000);

We also use the users.js file which can be seen below:



const express = require("express");
const router = express.Router();

router
.get("/", (req,res) => {
	res.send(users);
});

module.exports = router;

var users = [{
  "id": 1,
  "name": "Gerald"
}, {
  "id": 2,
  "name": "Linus"
}, {
  "id": 3,
  "name": "Aurelie"
}, {
  "id": 4,
  "name": "Beverlee"
}, {
  "id": 5,
  "name": "Sosanna"
}, {
  "id": 6,
  "name": "Will"
}];

Again here we need to require express. The var users object is a mock up object to experiment and when the user visits localhost:3000/ the list of users will be printed on the browser page. The user must be on the exact path of localhost:3000/. If the user wants this information to appear on an ‘users’ page for example, they must change the path name after the router.get request.

I hope this was an useful introduction and I will be working more on networking in the following weeks. Once again, thanks for reading!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s