Skip to content
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 @@ -6,5 +6,6 @@ npm-debug.log
package-lock.json
yarn-error.log
*.bkp
.env

week3/prep-exercise/server-demo/
19 changes: 19 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --env-file=../../.env server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"type":"module",
"dependencies": {
"express": "^5.1.0",
"express-handlebars": "^8.0.3",
"node-fetch": "^3.3.2"
}
}
32 changes: 32 additions & 0 deletions assignments/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import express from "express";
const PORT = process.env.PORT;
const app = express();
app.use(express.json());

app.get("/", (req, res) => {
res.send("hello from backend to frontend!");
});

app.post("/weather", (req, res) => {
const cityName = req.body.cityName;
if (!cityName) {
return res.status(400).send("City name is required");
}

res.send(`You entered: ${cityName}`);
});

app.use((err, req, res, next) => {
if (err) {
return res
.status(400)
.send(
"An error occurred while processing the data. Please check the entered data and try again"
);
}
next();
});

app.listen(PORT, () =>
console.log(`Server is running on http://localhost:${PORT}`)
);
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"nodemon": "^3.1.10"
}
}
1 change: 1 addition & 0 deletions week1/prep-exercises/1-web-server/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>My First Web Server</title>
</head>
<body>
Expand Down
35 changes: 30 additions & 5 deletions week1/prep-exercises/1-web-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,38 @@
* Exercise 3: Create an HTTP web server
*/

const http = require('http');
const http = require("http");
const fs = require("fs").promises;

//create a server
let server = http.createServer(function (req, res) {
// YOUR CODE GOES IN HERE
res.write('Hello World!'); // Sends a response back to the client
res.end(); // Ends the response
let server = http.createServer(async function (req, res) {
try {
if (req.url === "/") {
const html = await fs.readFile("index.html");
res.setHeader("Content-Type", "text/html");
res.write(html);
res.end();
} else if (req.url === "/index.js") {
const js = await fs.readFile("index.js");
res.setHeader("Content-Type", "application/javascript");
res.write(js);
res.end();
} else if (req.url === "/style.css") {
const css = await fs.readFile("style.css");
res.setHeader("Content-Type", "text/css");
res.write(css);
res.end();
} else {
res.statusCode = 404;
res.write("Not Found");
res.end();
}
} catch (error) {
res.statusCode = 500;
res.write("Server error");
res.end();
}
});

server.listen(3000); // The server starts to listen on port 3000

3 changes: 3 additions & 0 deletions week1/prep-exercises/1-web-server/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#content {
color: blue;
}