-
Notifications
You must be signed in to change notification settings - Fork 6
Assignments week 2 Ilias Khugaev #11
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import app from "../app.js"; | ||
import supertest from "supertest"; | ||
|
||
const request = supertest(app); | ||
|
||
describe("get /", () => { | ||
it(`it should return status code 200 and "Hello from backend to frontend!"`, (done) => { | ||
request.get('/') | ||
.expect(200, 'Hello from backend to frontend!', done) | ||
}); | ||
});`` | ||
|
||
|
||
describe("POST /weather", () => { | ||
it('should return status code 400 and "Missing name of city" if no cityName', (done) => { | ||
request.post('/weather') | ||
.send({ cityName: "" }) | ||
.expect(400, `{"error":"Missing name of city"}`, done) | ||
}); | ||
|
||
it('should return status code 200 and contain "City: Amsterdam" and "temperature"', (done) => { | ||
request.post('/weather') | ||
.send({ cityName: "Amsterdam" }) | ||
.expect(200) | ||
.expect(res => { | ||
if (!res.text.includes("City: Amsterdam")) { | ||
throw new Error('Response does not include "City: Amsterdam"'); | ||
} | ||
if (!res.text.toLowerCase().includes("temperature")) { | ||
throw new Error('Response does not include "temperature"'); | ||
} | ||
}) | ||
.end(done); | ||
}); | ||
|
||
it('should return status code 400 and "weatherText": "Error: city not found" if typing error', (done) => { | ||
request.post('/weather') | ||
.send({ cityName: "Amesterdam" }) | ||
.expect(400, `{"weatherText":"Error: city not found"}`, done) | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import express from "express"; | ||
import fetch from "node-fetch"; | ||
import keys 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.cityName; | ||
const responseText = { weatherText: "" } | ||
|
||
if (!cityName) { | ||
return res.status(400).json({ error: "Missing name of city" }); | ||
} | ||
|
||
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 (!response.ok) { | ||
const {message} = data; | ||
|
||
throw new Error(message); | ||
}; | ||
|
||
const {name, main} = data; | ||
responseText.weatherText = `City: ${name}, temperature: ${main.temp.toFixed(1)}°C`; | ||
res.status(200).send(responseText); | ||
|
||
} catch (error) { | ||
responseText.weatherText = `${error}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A suggestion here: Like this you are passing the error object as a response string. this can be dangerous as you dont know what error is being returned. It is better to return a custom message, like |
||
res.status(400).send(responseText); | ||
} | ||
|
||
}); | ||
|
||
export default app; |
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", | ||
}, | ||
}, | ||
], | ||
], | ||
}; |
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: [], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "hackyourtemperature", | ||
"version": "1.0.0", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "jest", | ||
"start": "node server.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"type": "module", | ||
"description": "", | ||
"dependencies": { | ||
"express": "^5.1.0", | ||
"node-fetch": "^3.3.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.27.1", | ||
"babel-jest": "^29.7.0", | ||
"jest": "^29.7.0", | ||
"supertest": "^7.1.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import app from './app.js'; | ||
|
||
const port = process.env.PORT || 3000; | ||
|
||
app.listen(port, () => console.log(`Listening on port ${port}`)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const keys = { | ||
API_KEY: | ||
} | ||
|
||
export default keys; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`` can be removed here