forked from HackYourFuture/Node.js
-
Notifications
You must be signed in to change notification settings - Fork 11
Lidya-w2-Node.js #16
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
lidyaT21
wants to merge
4
commits into
HackYourAssignment:main
Choose a base branch
from
lidyaT21:Lidya-w2-Node.JS
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lidya-w2-Node.js #16
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import app from "../app.js"; | ||
import supertest from "supertest"; | ||
|
||
const request = supertest(app); | ||
|
||
describe("POST /", () => { | ||
it("Quick test", () => { | ||
expect(1).toBe(1); | ||
}); | ||
}); | ||
|
||
describe("GET /", () => { | ||
it("Should return welcome text and status 200", async () => { | ||
const response = await request.get("/"); | ||
expect(response.status).toBe(200); | ||
expect(response.text).toBe("hello from backend to frontend!"); | ||
}); | ||
}); | ||
|
||
describe("POST /weather", () => { | ||
it("should return a 404 status code if city is not provided", async () => { | ||
const response = await request.post("/weather").send({}); | ||
|
||
expect(response.status).toBe(404); | ||
}); | ||
}); | ||
|
||
describe("POST /weather", () => { | ||
it("should return a 200 status code if city is provided", async () => { | ||
const response = await request | ||
.post("/weather") | ||
.send({ cityName: "Addis Ababa" }); | ||
|
||
expect(response.status).toBe(200); | ||
}); | ||
}); | ||
|
||
describe("POST /weather", () => { | ||
it("should return a 200 status code if city is provided and weather text", async () => { | ||
const response = await request | ||
.post("/weather") | ||
.send({ cityName: "Addis Ababa" }); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toHaveProperty("weatherText"); | ||
expect(response.body.weatherText).toContain("Addis Ababa"); | ||
}); | ||
}); | ||
|
||
describe("POST /weather", () => { | ||
it("should return a 500 status code if city is not found", async () => { | ||
const response = await request | ||
.post("/weather") | ||
.send({ cityName: "Addis Ababa!" }); | ||
|
||
expect(response.status).toBe(500); | ||
expect(response.body).toHaveProperty("weatherText"); | ||
expect(response.body.weatherText).toContain("City not found: Addis Ababa!"); | ||
}); | ||
}); | ||
describe("GET /nonexistent", () => { | ||
it("should return a 404 status code for non-existent routes", async () => { | ||
const response = await request.get("/nonexistent"); | ||
expect(response.status).toBe(404); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import express from "express"; | ||
import fetch from "node-fetch"; | ||
import keys from "./sources/keys.js"; | ||
const app = express(); | ||
|
||
app.use(express.json()); | ||
|
||
app.post("/weather", async (req, res) => { | ||
let { cityName } = req.body; | ||
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?&q=${cityName}&appid=${keys.API_KEY}`; | ||
if (!cityName) { | ||
return res.status(404).send("You need to provide a city name"); | ||
} | ||
try { | ||
const response = await fetch(apiUrl); | ||
if (!response.ok) { | ||
throw new Error("API not found "); | ||
} | ||
const data = await response.json(); | ||
const temp = data.main.temp; | ||
const tempCelsius = temp - 273.15; | ||
data.main.temp = tempCelsius; | ||
res.send({ | ||
weatherText: `The weather in ${cityName} is ${tempCelsius.toFixed( | ||
0 | ||
)} degrees Celsius.`, | ||
}); | ||
} catch { | ||
res.status(500).send({ weatherText: `City not found: ${cityName}` }); | ||
} | ||
}); | ||
|
||
app.get("/", (req, res) => { | ||
res.send("hello from backend to frontend!"); | ||
}); | ||
|
||
export default app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
], | ||
], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "hackyourtemperature", | ||
"version": "1.0.0", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "jest", | ||
"start": "node server.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"dependencies": { | ||
"express": "^4.21.2", | ||
"express-handlebars": "^8.0.1", | ||
"node-fetch": "^3.3.2", | ||
"nodemon": "^3.1.9" | ||
}, | ||
"type": "module", | ||
"devDependencies": { | ||
"@babel/core": "^7.26.9", | ||
"@babel/preset-env": "^7.26.9", | ||
"babel-jest": "^29.7.0", | ||
"jest": "^29.7.0", | ||
"supertest": "^7.0.0" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import app from "./app.js"; | ||
const port = 3000; | ||
app.listen(port, () => { | ||
console.log(`listening from server at port ${port}`); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const keys = { | ||
API_KEY: "b5dc5a484bf9f57400737317aeaf6e7f", | ||
}; | ||
|
||
export default keys; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,6 @@ | |
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.17.1" | ||
"express": "^4.21.2" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,51 @@ | ||
const express = require('express') | ||
const express = require("express"); | ||
const app = express(); | ||
|
||
|
||
// YOUR CODE GOES IN HERE | ||
app.get('/', function (req, res) { | ||
res.send('Hello World') | ||
}) | ||
|
||
app.listen(3000) | ||
const fs = require("fs"); | ||
app.use(express.json()); | ||
|
||
app.get("/", function (req, res) { | ||
res.send("Hello World"); | ||
}); | ||
|
||
//creating a blog | ||
app.post("/blogs", (req, res) => { | ||
const { title, content } = req.body; | ||
fs.writeFileSync(title, content); | ||
res.end("ok"); | ||
}); | ||
|
||
// updating a blog | ||
app.put("/blogs/:title", (req, res) => { | ||
const { title } = req.params; | ||
const { content } = req.body; | ||
if (fs.existsSync(title)) { | ||
fs.writeFileSync(title, content); | ||
res.end("ok"); | ||
} else { | ||
res.status(404).send("This blog does not exist!"); | ||
} | ||
}); | ||
|
||
//deleting a blog | ||
app.delete("/blogs/:title", (req, res) => { | ||
const { title } = req.params; | ||
if (fs.existsSync(title)) { | ||
fs.unlinkSync(title); | ||
res.end("ok"); | ||
} else { | ||
res.status(404).send("This blog does not exist!"); | ||
} | ||
}); | ||
|
||
//serving a blog | ||
app.get("/blogs/:title", (req, res) => { | ||
const { title } = req.params; | ||
if (fs.existsSync(title)) { | ||
const blogContent = fs.readFileSync(title, "utf8"); | ||
res.send(blogContent); | ||
} else { | ||
res.status(404).send("This blog does not exist!"); | ||
} | ||
}); | ||
|
||
app.listen(3000); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
exposing api key is a security risk. You should consider using dotenv module and store api keys in .env file in the root directory of the project.