Week 6 - Node & Express

Wednesday Agenda

  • Data relationship refinement

Resources

Monday

Tuesday

Thursday

The Resource Links page has a cheat sheet of common node and npm commands.

Node

Node in an environment for running JS outside of a web browser. To use JS as a server-side programming language, we need to use Node to execute our code. This video introduces it:

Reference

Node introduction

Modules

Modules are what we use to break JS code into separate files and libraries. Unhelpfully, there are two different standards for making modules. This video explains:

We are generally going to use ES6 modules. But as the video notes, MANY examples for Node use CommonJS modules. If you are researching how to do something and find a random tutorial on the web, you will likely have to adapt the syntax.

NPM is the tool we use to manage modules that have been bundled into a package:

Express

To build a web server, we need to create a program that listens for HTTP requests and responds to them. Node makes this pretty painless:

Express is a library that eliminates even more of the boilerplate code and lets us focus even more on just the logic of our application as opposed to the work going on at the HTTP level. It also serves as a platform that we can use to load all kinds of other libraries to help us build complex functionality:

Routing

Defining routes to handle different URLs is one of our primary tasks while setting up a web application. This video demonstrates some important techniques for writing and managing those route definitions:

Express References

EJS

Our application will need to render pages dynamically from data it retrieves or creates. To separate the logic of how to display the data on an HTML page we will use EJS - Embedded JavaScript templates. This video shows how EJS templates work:

If you are using HTMLHint, it will likely complain about the EJS files. Those files look like HTML, but have some sequences that are not valid HTML If every EJS tag shows an error that says Special characters must be escaped., you can add an .htmlhintrc file to tell HTMLHint to ignore that issue. Copy the one from the cs290Code repository and add it to your project folder.

This final video shows how we can take those EJS templates and make use of them inside an Express web app:

EJS Reference