Skip to content

Sertan-w2-Node.js #13

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 3 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ yarn-error.log
*.bkp

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

const request = supertest(app);

describe("POST /", () => {
it('should return 400 if no cityName provided', async() => {
const response = await request.post('/weather').send({});
expect(response.status).toBe(400);
expect(response.body).toEqual({ message: 'You have to provide a city name!' });
});

it('should return 404 if the cityName is not present in the data', async() => {
const response = await request.post('/weather').send({ cityName: 'ashjsdks'});
expect(response.status).toBe(404);
expect(response.body).toEqual({ weatherText: 'City is not found!' });
});

it('should return 200 if the cityName is found', async() => {
const response = await request.post('/weather').send({ cityName: 'Amsterdam' });
expect(response.status).toBe(200);
expect(response.body.weatherText).toContain('Amsterdam');
});
});

39 changes: 39 additions & 0 deletions assignments/config-files/hackyourtemperature/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import express 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.send('hello from backend to frontend!');
});

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

if (!cityName) {
return res.status(400).json({ message: 'You have to provide a city name!'});
}

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 weather = data.main.temp;

res.status(200).json({ weatherText: `The weather is ${weather}°C in ${cityName}` });
} catch (error) {
console.error('Error fetching data!');
res.status(500).json({ message: 'Something went wrong...' })
}
});

export default app;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
// 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",
Expand Down
27 changes: 27 additions & 0 deletions assignments/config-files/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"type": "module",
"main": "server.js",
"scripts": {
"test": "jest",
"start": "nodemon server.js"
},
"keywords": [],
"author": "Sertan Erdogan",
"license": "ISC",
"description": "",
"dependencies": {
"dotenv": "^16.4.7",
"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/config-files/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}`);
});
4 changes: 4 additions & 0 deletions assignments/config-files/hackyourtemperature/sources/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import dotenv from 'dotenv';
dotenv.config();

export const API_KEY = process.env.API_KEY;