Skip to content

Assignments week 1 Nikita #6

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 3 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
21 changes: 21 additions & 0 deletions hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"main": "server.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"dev": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^5.1.0",
"express-handlebars": "^8.0.3",
"node-fetch": "^3.3.2",
"nodemon": "^3.1.10"
}
}
25 changes: 25 additions & 0 deletions hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import express from 'express'
import { engine } from 'express-handlebars'
import fetch from 'node-fetch'


const app = express()
app.engine('handlebars', engine())
app.set('view engine', 'handlebars')
app.set('views', './views')

app.get('/', (req, res) => {
res.render('home')
})

app.use(express.json())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readability, we group functions together where possible.

In this case, put all middlewares before defining the routes. This makes it easier to find all the middlewares included in the program (and the order that they run in!)


app.post('/weather', (req, res) => {
const cityName = req.body.cityName

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good practice to check if a value exists when you aren't sure, in this example if the request does not have a body value (you can test this in Postman by setting the body to null https://gyazo.com/c075d8df776a091bc53bfa01ec459b80), then the error is returned to the user unhandled.
To improve this we can check if req.body?.cityName == undefined then return a clear error saying "city not found" (check res.status() for info on how to return an error status).

res.json({message: 'City name: ' + cityName})
})


app.listen(3000, () => {
console.log(`server started on http://localhost:3000`)
})
Empty file.
1 change: 1 addition & 0 deletions hackyourtemperature/views/layouts/main.handlebars

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have put the entire page in the "layout" section here. This works for one page, but if we had more, we could not reuse this layout system.

Layouts:
-> Defines the structure of page(s).

views:
-> Defines the specific content in the page.

For this example,

  • We would place "<h1>{{{body}}}</h1>" in the layouts/main.handlebars.
  • We would place "Hello from the backend to the frontend" in the home.handlebars file

This would mean we can reuse the layout if we had more than one view.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello from backend to frontend!</h1>
Empty file.
43 changes: 36 additions & 7 deletions week1/prep-exercises/1-web-server/server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
/**
* Exercise 3: Create an HTTP web server
*/

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

//create a server
let server = http.createServer(function (req, res) {
let server = http.createServer(async function (req, res) {
// YOUR CODE GOES IN HERE
res.write('Hello World!'); // Sends a response back to the client
res.end(); // Ends the response
});
try {
if (req.url === '/') {
const data = await fs.readFile(path.join(__dirname, 'index.html'))
res.writeHead(200, {
'Content-Type': 'text/html',
})
res.end(data)
} else if (req.url === '/index.js') {
const data = await fs.readFile(path.join(__dirname, 'index.js'))
res.writeHead(200, {
'Content-Type': 'application/javascript',
})
res.end(data)
} else {
res.writeHead(404, {
'Content-Type': 'text/plain',
})
res.end('404 not found')
}
} catch (err) {
res.writeHead(500, {
'Content-Type': 'text/plain',
})
res.end('server error')
console.error(err)
}
// res.write('Hello World!') // Sends a response back to the client
// res.end() // Ends the response
})

server.listen(3000); // The server starts to listen on port 3000
server.listen(3000, () => {
console.log('server is running (listen on port 3000)')
}) // The server starts to listen on port 3000