-
Notifications
You must be signed in to change notification settings - Fork 10
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
Nodejs week2 hackyourtemprature #27
base: main
Are you sure you want to change the base?
Changes from 4 commits
94b9453
1346564
0642593
bd0319c
04caf17
2cef0ba
5266660
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,28 @@ | ||
import app from "../app.js"; | ||
import supertest from "supertest"; | ||
|
||
const request = supertest(app); | ||
|
||
describe("POST /weather", () => { | ||
it("should return temperature 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"); | ||
}); | ||
|
||
it("should return error message for an invalid city", async () => { | ||
const response = await request | ||
.post("/weather") | ||
.send({ cityName: "InvalidCity" }); | ||
expect(response.status).toBe(404); | ||
expect(response.body.weatherText).toBe("City is not found!"); | ||
}); | ||
|
||
it("should return error for missing city name", async () => { | ||
const response = await request.post("/weather").send({}); | ||
expect(response.status).toBe(400); | ||
expect(response.body.error).toBe("City name is required."); | ||
}); | ||
}); |
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. Seems to be missing the code. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "hackyourtemperature", | ||
"version": "1.0.0", | ||
"description": "> If you are following the HackYourFuture curriculum we recommend you to start with module 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture curriculum first, click [here](https://github.com/HackYourFuture/curriculum).", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "jest", | ||
"start": "node server.js", | ||
"dev": "nodemon server.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.21.0", | ||
"express-handlebars": "^8.0.1", | ||
"node-fetch": "^3.3.2" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^3.1.7", | ||
"jest": "^29.0.0", | ||
"supertest": "^6.3.3", | ||
"babel-jest": "^29.0.0", | ||
"@babel/preset-env": "^7.20.0" | ||
} | ||
} |
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. Seems to be missing the code. |
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. You should add this file/folder to the project's git ignore list. 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. Thank you for letting me know, I added this to the gitignore 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const keys = { | ||
API_KEY: "005326ffeefecc610f41afaea641bfcd", | ||
}; | ||
export default keys; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"express": "^4.21.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,92 @@ | ||
const express = require('express') | ||
const express = require("express"); | ||
const fs = require("fs"); | ||
const app = express(); | ||
|
||
|
||
// YOUR CODE GOES IN HERE | ||
app.get('/', function (req, res) { | ||
res.send('Hello World') | ||
}) | ||
|
||
app.listen(3000) | ||
|
||
app.use(express.json()); | ||
|
||
app.get("/", (req, res) => { | ||
res.send("You have built you API 🎉"); | ||
}); | ||
|
||
app.post("/blogs", (req, res) => { | ||
const { title, content } = req.body; | ||
|
||
if (!title || !content) { | ||
return res.status(400).send("Title and content are required"); | ||
} | ||
|
||
try { | ||
fs.writeFileSync(`./${title}.txt`, content); | ||
res.status(201).send("Blog created"); | ||
} catch (err) { | ||
res.status(500).send("An error occurred while creating the blog"); | ||
} | ||
}); | ||
|
||
app.put("/blogs/:title", (req, res) => { | ||
const { title } = req.params; | ||
const { content } = req.body; | ||
|
||
if (!content) { | ||
return res.status(400).send("Content is required"); | ||
} | ||
|
||
if (fs.existsSync(`./${title}.txt`)) { | ||
try { | ||
fs.writeFileSync(`./${title}.txt`, content); | ||
res.send("Blog updated"); | ||
} catch (err) { | ||
res.status(500).send("An error occurred while updating the blog"); | ||
} | ||
} else { | ||
res.status(404).send("This post does not exist!"); | ||
} | ||
}); | ||
|
||
app.delete("/blogs/:title", (req, res) => { | ||
const { title } = req.params; | ||
|
||
if (fs.existsSync(`./${title}.txt`)) { | ||
try { | ||
fs.unlinkSync(`./${title}.txt`); | ||
res.send("Blog deleted"); | ||
} catch (err) { | ||
res.status(500).send("An error occurred while deleting the blog"); | ||
} | ||
} else { | ||
res.status(404).send("This post does not exist!"); | ||
} | ||
}); | ||
|
||
app.get("/blogs/:title", (req, res) => { | ||
const { title } = req.params; | ||
|
||
if (fs.existsSync(`./${title}.txt`)) { | ||
try { | ||
const content = fs.readFileSync(`./${title}.txt`, "utf8"); | ||
res.send(content); | ||
} catch (err) { | ||
res.status(500).send("An error occurred while reading the blog"); | ||
} | ||
} else { | ||
res.status(404).send("This post does not exist!"); | ||
} | ||
}); | ||
|
||
app.get("/blogs", (req, res) => { | ||
try { | ||
const files = fs.readdirSync("./"); | ||
const blogs = files | ||
.filter((file) => file.endsWith(".txt")) | ||
.map((file) => ({ | ||
title: file.replace(".txt", ""), | ||
})); | ||
res.json(blogs); | ||
} catch (err) { | ||
res.status(500).send("An error occurred while reading the blogs"); | ||
} | ||
}); | ||
|
||
app.listen(3000, () => { | ||
console.log("Server is running on http://localhost:3000"); | ||
}); |
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.
These look good in principle. However, I'll only be able to say with certainty once you commit also the contents of the
app.js
file :)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.
Sorry about that, Michal. I have written the code, but I forgot to double-check that it has not been committed🤔 I have just discovered that I should go to the file command + s and then it would be somehow added the I should add it to git and commit that why the file was empty