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
22 changes: 22 additions & 0 deletions assignments/HackYourTemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"description": "HackYourTemperature project - Week 1 setup",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
},
"keywords": [],
"author": "Majd Jad_alhaq",
"license": "ISC",
"dependencies": {
"express": "^4.21.1",
"express-handlebars": "^8.0.3",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"nodemon": "^3.1.7"
}
}
37 changes: 37 additions & 0 deletions assignments/HackYourTemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// server.js

// Import necessary packages (using ES modules)
import express from "express";
import fetch from "node-fetch"; // not used yet, but needed later
import { engine } from "express-handlebars";

// Create an Express application
const app = express();

// Middlewares
app.use(express.json()); // allows Express to read JSON in POST requests

// Optional: set up handlebars for later weeks
app.engine("handlebars", engine());
app.set("view engine", "handlebars");
app.set("views", "./views");

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

// POST route
app.post("/weather", (req, res) => {
// Extract cityName from the JSON body
const { cityName } = req.body;

// Send it back to the client
res.json({ message: `You sent: ${cityName}` });
});

// Listen to port 3000
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
41 changes: 35 additions & 6 deletions week1/prep-exercises/1-web-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,41 @@
*/

const http = require('http');
const fs = require('fs');
const path = require('path');

//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
// Create the server
const server = http.createServer((req, res) => {
if (req.url === '/') {
// Serve the index.html file
fs.readFile(path.join(__dirname, 'index.html'), (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Server Error');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
} else if (req.url === '/index.js') {
// Serve the index.js file
fs.readFile(path.join(__dirname, 'index.js'), (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('File Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.end(data);
}
});
} else {
// Handle any other request with 404
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
});

server.listen(3000); // The server starts to listen on port 3000
// Start listening on port 3000
server.listen(3000, () => {
console.log('✅ Server running at http://localhost:3000');
});