-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,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" | ||
} | ||
} |
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()) | ||
|
||
app.post('/weather', (req, res) => { | ||
const cityName = req.body.cityName | ||
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. 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. |
||
res.json({message: 'City name: ' + cityName}) | ||
}) | ||
|
||
|
||
app.listen(3000, () => { | ||
console.log(`server started on http://localhost:3000`) | ||
}) |
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 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: views: For this example,
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> |
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 |
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.
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!)