Node 3
What we will learn today?
Node Recap
Javascript has evolved from the browser to be used in the backend. It does not need a framework as we saw in week-13, however a framework like Express allows us to develop faster.
Documentation
When you are working with frameworks it is always helpful to use their documentation.
Middleware
Middleware allow us to process requests to add functionalities that are not built in to Express, for example logging, authentication, etc.
- Express Documentation: Using Middleware
- Video: body-parser which makes it easier to work with POST requests and forms.
Routing
Routing refers to how an application’s endpoints (URIs) respond to the client requests. These are configured differently for each framework, and can range from basic configuration to very extensive for more complex use cases.
Simple example
app.get("/", function(req, res) {
res.send("hello world");
});
More complicated example using Passport.js
middleware for authentication
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) return next();
else res.redirect("/login");
}
app.get("/account", ensureAuthenticated, function(req, res) {
res.send("welcome user!");
});
- Express Documentation: Routing
Best Practices
Express have their own recommended best practices page
Node Process Managers
These process managers monitor for any changes in your node.js application and automatically restart the server, perfect for development and production.
Popular PMs include:
REST
REST is a convention of how to design your API, whether it is for your own frontend, or other frontends and clients.
- Read: Resource naming in REST convention
Security
When you take your website to production there is a whole range of things to consider.
Node has built in support for HTTPS, and once you have your own certificates you can use this feature. However sometimes when you deploy to cloud
Platform Platform as a Service (PaaS) providers such as Heroku, they provide SSL and configure it for you automatically.
- Read: Node: HTTPS
- Read/Watch: What is an SSL Certificate?
- Read: Heroku SSL
Authentication
Sometimes you need to add user functionality for your website. To do this you can make use of external libraries like passport to add authentication to your website.
For more information read:
Hotel Workshop
Get in to groups of 3/4 and checkout the workshop brief.
Exercise: Please fork and clone CYF-Hotel repository and follow the exercises in the
README.MD
.
Homework
- Finish the hotel workshop with your team, assigning trello tickets in your team trello page
Prepare for the next class
- Read: Persistence (computer science)
- Watch: What is Database & SQL?
- Learn some basic SQL with khanacademy or codecademy and ask any questions in the channel
Escalation policy
When you get stuck during the homework you should follow the following:
- First, search for answers on Google and Stack Overflow. If you have an error message, copy and paste it into Google.
- When you are stuck for longer than 20 minutes - no longer! - write a question to your fellow students in your class channel on slack. Take a while to think carefully about the question. This is an important skill. There is a great guide to writing good questions here
- If you cannot get an answer from your fellow students, post your question in your class public channel, @ the mentor for the class that covered the topic and we will reply as soon as we can.