Please note that what we are building is very simple but the concepts we touch on are the same ones used to build larger applications.

This is a two part series. You can find part two here. In this part we will only be the back-end part of our app

Step 1: Project Setup

Before you can dive deep into the tutorial you will need the following:

Node (I am using v10.11.0)

npm (I am using 6.4.1)

A Text Editor (I am using VS Code)

A browser (I definitely recommend Chrome or Firefox)

Note that you do not need to have the exact version of Node and npm as I have for this to work. If you get any trouble getting these configured then please comment below and I will be happy to help. Once you have these setup its time to download or clone the starter code. You can find the files on my Github. Don’t worry if you don’t understand what every file in the repository does, we will walk through each relevant file and its function.

Step 2: Understand the starter files

If you successfully cloned the starter code repository then this is the file structure you should have:

/Fake-Weather-API

.gitignore

README.md

data.js

index.js

package-lock.json

package.json

First things first, lets check out our package.json file

{

"name": "fake_weather_api_starter",

"version": "1.0.0",

"description": "",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "Maher Alkendi",

"license": "ISC",

"dependencies": {

"express": "^4.16.4"

}

}

This file contains the main information about our app. Note that we only have one dependency, which is express . Express is a Web Framework that makes it easy to build server-side applications using node. Read more about it here.

Next we look at our index.js file

const express = require('express'); // import express

const app = express(); // initialize express // Listen on port 3000

app.listen(3000, function() {

console.log('listening on port 3000...');

})

All this code does is initialize an express server on port 3000. We will add more functionality to it as we go.

Now lets checkout the data.js file

const fakeWeatherData = [

{

'city': 'muscat',

'temperature (C)': '37 C',

'temperature (F)': '98.6 F',

},

{

'city': 'tokyo',

'temperature (C)': '16 C',

'temperature (F)': '61 F',

},

{

'city': 'moscow',

'temperature (C)': '18 C',

'temperature (F)': '64 F',

},

{

'city': 'sydney',

'temperature (C)': '17 C',

'temperature (F)': '63 F',

},

{

'city': 'london',

'temperature (C)': '16 C',

'temperature (F)': '66 F',

},

{

'city': 'johannesburg ',

'temperature (C)': '24 C',

'temperature (F)': '75 F',

},

]; module.exports = fakeWeatherData;

This file contains our fake weather data. It contains an array of objects called fakeWeatherData . Each object has a key for city, temperature in Celsius and temperature in Fahrenheit. At the bottom of the file we are exporting this array. Please note that this is fake data that I made up. In a real application this information will most likely be coming from a database.

In addition, we have our .gitignore files which ignores our node_modules folder.

OK these are the main files of the starter code. Now lets start building our fake weather API!

Step 3: Build Fake Weather API

Before we start writing any code, don’t forget to type npm install on your terminal to install all dependencies (in this case its only express). Now open up the index.js on your Text editor. Lets start by making our app return some json when it gets a GET request on the root directory.

const express = require('express'); // import express

const app = express(); // initialize express // GET route

app.get('/', function(req, res) {

res.send({'hello': 'world'})

}) // Listen on port 3000

app.listen(3000, function() {

console.log('listening on port 3000...');

})