Skip to content

Assignments week 1 YANA SENIUK #5

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 8 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
4 changes: 4 additions & 0 deletions assignments/hackyourtemperature/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
.env
.vscode
npm-debug.log
18 changes: 18 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"description": "",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node src/server.js",
"dev": "node --watch --env-file=.env src/server.js"
},
"author": "yanaesher",
"license": "ISC",
"devDependencies": {
"express": "^5.1.0",
"express-handlebars": "^8.0.3",
"node-fetch": "^3.3.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function getCityName(req, res, next) {
const { cityName } = req.body;

if (!cityName?.trim()) {
const error = new Error("Required parameter 'cityName' is missing");
error.status = 400;
return next(error);
}

res.status(200).json({ cityName });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function errorHandler(err, req, res, next) {
const statusCode = err.status || 500;
const message = err.message || "Something went wrong";
res.status(statusCode).json({ message });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function notFound(req, res, next) {
const error = new Error(`Not found`);
error.status = 404;
next(error);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function validateBody(req, res, next) {
if (!req.body || Object.keys(req.body).length === 0) {
const error = new Error("Request body is missing");
error.status = 400;
return next(error);
}

next();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from "express";
import { validateBody } from "../middlewares/validateBody.js";
import { getCityName } from "../controllers/weather.controller.js";
const router = Router();

router.post("/", validateBody, getCityName);

export default router;
23 changes: 23 additions & 0 deletions assignments/hackyourtemperature/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import express from "express";
import weatherRouter from "./routes/temperature.routes.js";

import { notFound } from "./middlewares/notFound.middleware.js";
import { errorHandler } from "./middlewares/error.middleware.js";

const app = express();
const PORT = process.env.PORT || 3000;

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use("/api/weather", weatherRouter);
app.get("/api", (req, res) => {
res.status(200).send("hello from backend to frontend!");
});

app.use(notFound);
app.use(errorHandler);

app.listen(PORT, () => {
console.log(`Server is running at http://127.0.0.1:${PORT}`);
});