Skip to content
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: 2 additions & 0 deletions assignments/HackYourTemperature/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.env
43 changes: 43 additions & 0 deletions assignments/HackYourTemperature/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import express from "express";
import keys from "./sources/keys.js";

const app = express();
app.use(express.json());

app.get("/", (req, res) => {
res.send("Hello, from backend to frontend!");
});

app.post("/weather", async (req, res) => {
const { cityName } = req.body;

if (!cityName) {
return res.status(400).json({
weatherText: "City name is required!",
});
}

try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${keys.API_KEY}&units=metric`,
);

const data = await response.json();

if (data.cod === "404") {
return res.status(404).json({
weatherText: "City is not found!",
});
}

res.json({
weatherText: `The temperature in ${data.name} is ${data.main.temp}°C.`,
});
} catch (error) {
res.status(500).json({
weatherText: "Error fetching weather data.",
});
}
});

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",
},
},
],
],
};
9 changes: 9 additions & 0 deletions assignments/HackYourTemperature/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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: [],
testEnvironment: "node",
};
29 changes: 29 additions & 0 deletions assignments/HackYourTemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"description": "",
"main": "server.js",
"type": "module",
"scripts": {
"test": "jest",
"start": "node server.js",
"prettier:check": "prettier --check .",
"prettier:write": "prettier --write ."
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^17.2.3",
"express": "^5.1.0",
"express-handlebars": "^8.0.3"
},
"devDependencies": {
"@babel/preset-env": "^7.28.5",
"babel-jest": "^30.2.0",
"husky": "^9.1.7",
"jest": "^30.2.0",
"lint-staged": "^16.2.6",
"supertest": "^7.1.4"
}
}
7 changes: 7 additions & 0 deletions assignments/HackYourTemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import app from "./app.js";

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
8 changes: 8 additions & 0 deletions assignments/HackYourTemperature/sources/keys.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEVER EVER EVER UPLOAD YOUR API KEYS!!!!!!!!!! Instead put them either in an .env file, or use them as runtime arguments (API_KEY=123245 node server.js and then process.env.API_KEY in code)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it and add the last modification commit, it is very useful feedback because i made a big error with sharing my API_key

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import dotenv from 'dotenv';
dotenv.config();

const keys = {
API_KEY: process.env.API_KEY
};

export default keys;
28 changes: 28 additions & 0 deletions assignments/HackYourTemperature/tests/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import app from "../app.js";
import supertest from "supertest";

const request = supertest(app);

describe("POST /weather", () => {
it("should return 400 if no cityName is provided", async () => {
const response = await request.post("/weather").send({});
expect(response.status).toBe(400);
expect(response.body.weatherText).toBe("City name is required!");
});

it("should return 404 if city is not found", async () => {
const response = await request
.post("/weather")
.send({ cityName: "UnknownCity" });
expect(response.status).toBe(404);
expect(response.body.weatherText).toBe("City is not found!");
});

it("should return weather data for a valid city", async () => {
const response = await request
.post("/weather")
.send({ cityName: "Amsterdam" });
expect(response.status).toBe(200);
expect(response.body.weatherText).toContain("Amsterdam");
});
});