JavaScript

History

Basics

Key features / differences

Express

  1. npm init (accept defaults)
  2. npm install express --save
  3. npm install ejs --save (skip if you prefer to use Pug)
  4. Create index.js with the following content:

     const express = require('express')
     const app = express()
     const port = 3000
        
     app.get('/', (req, res) => res.send('Hello World!'))
        
     app.listen(port, () => console.log(`Example app listening on port ${port}!`))
    
  5. Launch node index.js
    • visit localhost:3000
  6. Create a directory named views
    • By default, templates go here.
    • Convention vs. configuration.
    • If you want to change it: app.set('views', './your_preferred_location')
  7. Create a file in views named hello.ejs (or hello.pug, if you prefer)

    <p>Hello, there <%= name %></p>
    <p>Not a bad first page.</p>
    
  8. Add app.get('/', (req, res) => res.render('hello.ejs', { name: "Bob" })); to index.js
    • You can also set a default template engine: app.set('view engine', 'ejs')
    • Doing so allows you to leave off the .ejs
  9. Launch the server and visit the root page

  10. Add a route
    • print the req object.
    • print just req.query
    • Use req.query set a variable in the template

MVC (Model, View, Controller)

Convention over configuration

Testing / Jest

Mocking

Databases / Asynchronous programming / Promises