Skip to content

Feras Aldemashki w2 node #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ npm-debug.log
package-lock.json
yarn-error.log
*.bkp

keys.js
week3/prep-exercise/server-demo/
41 changes: 41 additions & 0 deletions assignments/hackyourtemperature/__tests__/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import app from "../app.js";
import request from "supertest";

describe("GIT /", () => {
describe("send a welcome message to frontend", () => {
test("should respond with a 200 status and say hello to frontend", async () => {
const response = await request(app).get("/");

expect(response.statusCode).toBe(200);
expect(response.text).toEqual("hello from backend to frontend!");
});
});
});

describe("POST /weather", () => {
describe("given a valid city name", () => {
test("should respond with a 200 status", async () => {
const response = await request(app).post("/weather").send({
cityName: "London",
});
expect(response.statusCode).toBe(200);
expect(response.body).toContain("London");
});
});
describe("given an unvalued city name ", () => {
test("should respond with a 404 status code if no city found in data", async () => {
const response = await request(app).post("/weather").send({
cityName: "anything except a real city ",
});
expect(response.statusCode).toBe(404);
expect(response.body).toEqual({ weatherText: "City is not found!" });
});
});
describe("when city name is missing", () => {
test("should respond with a 400 status code if no city name provided", async () => {
const response = await request(app).post("/weather").send({});
expect(response.statusCode).toBe(400);
expect(response.body).toEqual({ message: "no city name provided" });
});
});
});
32 changes: 32 additions & 0 deletions assignments/hackyourtemperature/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import express, { response } from "express";
import fetch from "node-fetch";
import { API_KEY } from "./sources/keys.js";

const app = express();
app.use(express.json());
app.get("/", (req, res) => {
res.status(200).send("hello from backend to frontend!");
});
app.post("/weather", async (req, res) => {
const { cityName } = req.body;
if (!cityName) {
return res.status(400).json({ message: "no city name provided" });
}
try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric`
);
if (!response.ok) {
return res.status(404).json({ weatherText: "City is not found!" });
}

const data = await response.json();
const { temp } = data.main;

res.status(200).json(`the temp in ${cityName} is: ${temp}`);
} catch (error) {
console.log(error);
res.status(500).json({ message: error });
}
});
export default app;
13 changes: 13 additions & 0 deletions assignments/hackyourtemperature/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
presets: [
[
// This is a configuration, here we are telling babel what configuration to use
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
],
};
8 changes: 8 additions & 0 deletions assignments/hackyourtemperature/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
// Tells jest that any file that has 2 .'s in it and ends with either js or jsx should be run through the babel-jest transformer
transform: {
"^.+\\.jsx?$": "babel-jest",
},
// By default our `node_modules` folder is ignored by jest, this tells jest to transform those as well
transformIgnorePatterns: [],
};
25 changes: 25 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "week2",
"version": "1.0.0",
"description": "## Agenda",
"main": "server.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "Feras Aldemashki",
"license": "ISC",
"type": "module",
"dependencies": {
"express": "^4.21.2",
"express-handlebars": "^8.0.1",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"@babel/preset-env": "^7.26.9",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"nodemon": "^3.1.9",
"supertest": "^7.0.0"
}
}
6 changes: 6 additions & 0 deletions assignments/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import app from "./app.js";
const port = 3000;

app.listen(port, () => {
console.log(`server is running on port: ${port}`);
});