Skip to main content

Node - 1

Learning Objectives

By the end of this lesson trainees should be able to:

  • Define what an API is used for
  • Create express servers locally
  • Use npm to start a node server
  • Explain what express is and what it is used for
  • Use express to create an API that will accept a GET request that returns JSON
  • Implement routing to return different resources depending on URL
  • Implement query params to return different content (?query=ses)

Before you start

Before you arrive at class you must have completed the following setup work.

Node Preparation

If you are a teacher reading this, please check the Instructor Notes

1) API Refresh

Exercise

Exercise: Discuss known APIs

  1. Groups discuss any APIs they know about.
  2. What might you do with it?
  3. Announce answers to class.

Before we make our own API, let's look at and use some other APIs.

Here's an example API that shows us the Sunrise and Sunset times of everywhere in the world.

Next let's try an exercise

Group Exercise (5 minutes)

So what's the big deal? I can see this information on web sites already!

What's an API and how is it different from a web site?

2) Introduction to Node

2.1) Investigating a Simple Server

Let's investigate a web server made in Node. You can check it out running here:

Teacher-Led Live Coding Example

Let's inspect the different parts of the Node App and how Express works. Let's discuss

  • require on Line 3
  • express() on Line 5
  • app.get() on Line 12, 26 and 20
  • app.listen() on Line 29
  • response.send() on Line 13 and Line 17
  • response.json() on Line 22

Can we work out what each those lines are doing?

Some examples here use Glitch, a fun code playground you might like to explore. We will learn about deploying Node applications and making them available for others, but we will use Render.

2.2) Running the Simple Server locally

Fork and clone: https://github.com/CodeYourFuture/Node-Exercises.

This repository contains multiple small projects, so enter the cyf-simple-express directory where we will be doing our work today.

Once cloned we're going to use the Node Package Manager (npm) to setup the project using the npm install command.

NPM is the place to go to download other Node code written by other people. There are thousands of open-source, 3rd-party Node modules (also known as packages) written by other people that you can download and use in your own projects.

Run the following command in your terminal:

npm install

Once the prerequsities are installed you can now start the server by typing

npm start

Once it starts up you will be able to access your running API by going to

http://localhost:3000

2.3) Modifying the Simple Server

Exercise:

Let's try to modify the server.js code above to do something different!

Examples:

  1. Say "Hello Miles", instead of "Hello Kash"
  2. Make it return an array of strings as json.
  3. Make it return the current time
  4. Advanced: make it return whatever you want! 3 minutes.

3) Make a Node API

The rest of today's lesson is a workshop.

In small groups complete each of the steps below. At different points, the teacher will introduce new concepts.

3.1) Get Started

Fork and clone the repo https://github.com/CodeYourFuture/Node-Starter-Kit.

It is an empty project that includes all the details you need to get started building your first Node App.

Once cloned, make sure to run the following code to install all prerequisites.

npm install

3.2) Building the server

The first thing we need to do is build our server. You will always need to build a server when writing back-end code. A server can be built in pure Node.js, but Express is simpler to work with.

1. Create a server.js file

Let's build our server! Before we do anything, let's create a new file called server.js. This is where all our server code is going to live.

2. require the express library

We already installed Express in Step 2, but we need to make sure it is included in this file specifically so we can use its methods. In Node.js, when you want to use a package in another file, you must require it.

To require Express, write the following inside server.js:

const express = require("express");

Let us get used to ES6 syntax - so use const and let instead of var, arrow methods instead of functions, etc...

3. Initialise the server

To initialise our server, we need to call the express() function. This will create an Express application for us to work with.

Add the second line of code to your server.js file:

const express = require("express");
const app = express();

4. Start 'listening' for potential requests

One more step left, we need to set a port for our server to listen to. Think of a port as a door number; any requests that come to the server will come via that door. Setting a port will allow us to find where our server is running.

We use the app.listen method to do this. This method takes two arguments: a port and a callback function telling it what to do once the server is running.

Need clarification? Read more about the app.listen method in the Express documentation.

We're going to run our server on port 3000, and add a simple console.log in the callback function. Update your server.js file, calling the app.listen method:

const express = require("express");
const app = express();

app.listen(3000, function () {
console.log("Server is listening on port 3000. Ready to accept requests!");
});
Task

Try to use ES6 arrow functions instead of function.

5. Switch the server on!

You've built your server, but it isn't running yet. We need to run a command in the terminal to do this. We are going to use the node keyword to run the server file.

Type the following command in your terminal:

node server.js

If you see this, congratulations! You have built yourself a server!

success

6. npm script

To exit the running the server, type crtl + c. Instead of running the server with node server.js everytime, we can create an alias for it in package.json.

Under the scripts property, add start: node server.js. We can now run our server using npm start which will be an alias (a shortcut) to node server.js.

Go to the terminal and type npm start and make sure that the server still runs.

3.3) Communicating with the server

Now that we've built the server, we need to communicate with it. We are going to control the server with handler functions.

What is a handler function?

When a request reaches the server, we need a way of responding to it. In comes the handler function. The handler function is just a function which receives requests and handles them, hence the name.

The handler function is always called with a request and response object. The response object is what gets sent back to the client. It contains the information that gets displayed in the web page. You can decide what to send back in your response.

What does a handler function look like in Express?

The get() method is one of the methods used to define a handler function in Express. It takes two parameters: the endpoint at which to trigger an action (we'll explain more about this in the next step), and the handler function that tells it exactly what to do. Here's a simple "Hello World!" example:

// req is the Request object, res is the Response object
// (these are just variable names, they can be anything but it's a convention to call them req and res)
app.get("/", function (req, res) {
res.send("Hello World!");
});

Here, we are telling our server to respond with "Hello World!" when someone tries to access the webpage.

1. Create your own handler function

Let us add a handler handler function to send back a message to the client. To do that, we're going to use the Express send() method. This will update the response object with the message.

Update your handler function like so:

const express = require("express");
const app = express();

app.get("/", function (req, res) {
res.send("Yay Node!");
});

app.listen(3000, function () {
console.log("Server is listening on port 3000. Ready to accept requests!");
});

::: Break Exercise Try to console.log the request object inside the handler function.

Restart your server, send the request again with Postman, then go to your terminal to see what it looks like. You should see a lot of data come through. :::

2. Check it out in Postman

Quit your server in the terminal with ctrl + c. Then restart it to run your new changes.

node server.js

Now, open Postman, and send a GET request to http://localhost:3000. If you see your message in Postman, congratulations! You just sent your first response from the server.

Checkpoint

Do you understand all these terms? You should be able to see examples of them in Postman

  • Client
  • Server
  • Front-end
  • Back-end
  • Full stack developer
  • Request
  • Response
  • HTTP
  • HTTP Response Codes
  • HTTP Request Methods (or Verbs)

3.4) Routing

At the moment our server only does one thing. When it receives a request from the / endpoint, it sends back the same response: "Yay Node!".

Exercise

Try typing http://localhost:3000/node and see what happens.

However by making use of endpoints, we can make the server send different responses for different requests. This concept is called routing.

What is an endpoint?

An endpoint is the part of the URL which comes after /. For example: /chocolate is the "chocolate" endpoint. It's the URL to which you send a request.

What is URL?

alt text

Create your own endpoints and send different responses

We're going to try sending different responses at different endpoints. Remember the app.get() method? To set up routing in your server, we just need to repeat this method with different endpoints.

For example:

app.get("/", function (req, res) {
res.send("Hello World!");
});

app.get("/chocolate", function (req, res) {
res.send("Mm chocolate :O");
});
Exercise

Add some code so that your server sends one message when the endpoint is /node and another one when it's /codeyourfuture.

3.5) Query Parameters

So, what is a query parameter?

In simple terms, a query string is the part of a URL (Uniform Resource Locater) after the question mark (?). It is meant to send small amounts of information to the server via the url. This information is usually used as parameters to query a database, or maybe to filter results. It's really up to you what they're used for.

Here is an example of a URL with query strings attached:

https://stackabuse.com/?page=2&limit=3

1. Detect Query Parameters

We're going to try sending different responses at different endpoints. Remember the app.get() method? To set up routing in your server, we just need to repeat this method with different endpoints.

For example:

app.get("/", function (req, res) {
let searchQuery = req.query.search;
res.send("Hello World! You searched for " + searchQuery);
});

Here is an example of how we would use this endpoint with query parameters:

http://localhost:3000?search=hello

Now your turn!

Exercise

Add some code so that your server returns the amount of chocolate that you want from /chocolate endpoint. For example

http://localhost:3000/chocolate?amount=3

2. Multiple Query Parameters

What if we want to detect and read multiple parameters? If we use a URL from earlier as an example, here is how we would send multiple query parameters:

https://api.sunrise-sunset.org/json?lat=51.5311&lng=0.0481

Here we have one parameter called "lat" and another "lng".

Here is how we would do that in Node:

app.get("/json", function (req, res) {
let lat = req.query.lat;
let lng = req.query.lng;
res.send(`You searched for Lat: ${lat} and Lng: ${lng}`);
});
Exercise

Add some code so that your server takes 2 values that we will multiply together and return the value For example

http://localhost:3000/multiply?value1=2&value2=10

This should return value of 20

PD (Professionalism)

Adapted from Tectonica workshop

Session objective: This session explores the professional conduct, behaviour and attitude required in the work place. We will talk about the accepted standards of professionalism and the myths about behaving professionally.

Professionalism slides

Exercise (10 min)

Exercise objective: To communicate with colleagues with a professional attitude

  1. On your own write a Slack message to a team member saying you're running late to a meeting. Volunteer to share your message with the class.
  2. On your own write an email to a manager notifying them that you'll be out of the office on some future date. Volunteer to share your message with the class.
Exercise (10 min)

Exercise objective: To role-play a conversation between a team member and manager about project deadline

Pair with another trainee. Take turns role-playing the following scenario:

One of you is a team member and you need to tell your manager in person that your project is likely not going to be finished by the agreed deadline. The other person is the manager and should express unhappiness about this, but also work together to come up with a new, reasonable deadline or some other alternative (e.g. reduce the scope of the project or get help from a team member). Be sure to switch roles.

Feedback

Please spend two minutes reviewing this lesson to help us improve it for the future. This feedback will be shared with volunteers.