ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Follow publication

Learn how Full-stack apps work by building a simple one using JavaScript (Part 1)

Maher Alkendi
ITNEXT
Published in
8 min readNov 17, 2018

--

image by Clément H

Step 1: Project Setup

Step 2: Understand the starter files

/Fake-Weather-API
.gitignore
README.md
data.js
index.js
package-lock.json
package.json
{
"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"
}
}
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...');
})
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;

Step 3: Build Fake Weather API

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...');
})
const express = require('express');   // import express
const app = express(); // initialize express
const fakeWeatherData = require('./data.js'); // import fake data
// GET route
app.get('/', function(req, res) {
res.send(fakeWeatherData[0]);
})
app.listen(3000, function() {
console.log('listening on port 3000...');
})
const express = require('express');  // import express
const app = express(); // initialize express
// import our fake weather data
const fakeWeatherData = require('./data.js');
// GET route
app.get('/weather', function(req, res) {
// if no city parameter exists
if (!req.query.city) {
res.send({"status": "error", "message": "Please enter a city name"})
} else {
res.send(fakeWeatherData[0])
}
});
app.listen(3000, function() {
console.log('listening on port 3000...');
})
const express = require('express');  // import express
const app = express(); // initialize express
// import our fake weather data
const fakeWeatherData = require('./data.js');
// GET route
app.get('/weather', function (req, res) {
// store the query string parameter in cityName variable
let cityName = req.query.city.toLowerCase();
// Loop through our fake data array
for (let i = 0; i < fakeWeatherData.length; i++) {
// if no city parameter exists
if (!cityName) {
return res.send({"status": "error", "message": "Please enter a city name"})
} else if (cityName == fakeWeatherData[i].city.toLowerCase()) {
return res.send(fakeWeatherData[i])
}
}
// if city parameter isn't in our fake data set
res.send({"status": "error", "message": "This city isn't in our database"})
});app.listen(3000, function () {
console.log('listening on port 3000...');
})
npm i --save cors
{
"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": {
"cors": "^2.8.4",
"express": "^4.16.4"
}
}
const express = require('express');  // import express
const app = express(); // initialize express
const cors = require('cors'); // import cors package
// import our fake weather data
const fakeWeatherData = require('./data.js');
// Enable all CORS requests
app.use(cors());
app.get('/weather', function (req, res) {// store the query string parameter in cityName variable
let cityName = req.query.city.toLowerCase();
// Loop through our fake data array
for (let i = 0; i < fakeWeatherData.length; i++) {
// if no city parameter exists
if (!cityName) {
return res.send({"status": "error", "message": "Please enter a city name"})
} else if (cityName == fakeWeatherData[i].city.toLowerCase()) {
return res.send(fakeWeatherData[i])
}
}
// if city parameter isn't in our fake data set
res.send({"status": "error", "message": "This city isn't in our database"})
});app.listen(3000, function () {
console.log('listening on port 3000...');
})

--

--

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Responses (5)