Creating our project.
suggest changeFrom the console, create the project directory:
mkdir our_project
cd our_project
Now the shell is in the directory where the code will live.
How do we create the Express skeleton project?
Install the generator, not a global copy of Express itself:
npm install -g express-generator
On Linux and macOS you may need sudo, because a global install writes outside your home directory. Then create the skeleton:
express
That command creates an example app in the current folder. The structure is as follows:
bin/
public/
routes/
views/
app.js
package.json
Install the dependencies the generator listed, then start the app. Open http://localhost:3000 and the example page is served. Older generator templates use Jade. Current ones already use Pug.
npm install
npm start
How express works, briefly?
Express is a framework built on Node.js. The docs are on the Express site. For this project, the important part is that Express chooses what to render for a URL such as http://localhost:3000/home. The generated app does it here:
FILE: routes/index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
A request for http://localhost:3000 renders the index view and passes { title: 'Express' }. That is a view local, not a JSON response. views/index.jade looks like this:
extends layout
block content
h1= title
p Welcome to #{title}
Template engines render those variables, and a template can extend another one. The extension here is .jade. Jade was renamed to Pug. It is the same engine, with later updates.
Installing Pug and updating Express template engine.
To use Pug as the template engine, run:
npm install --save pug
That installs Pug and saves it in package.json. If the generator already set the view engine to pug, leave app.js alone. A Jade template needs the engine switched in app.js:
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
Rename views/*.jade to views/*.pug if the files are still Jade. npm start should serve the same pages.
How AngularJS fits in all of this?
AngularJS is a JavaScript MVW (model-view-whatever) framework often used for a SPA (single-page application). Download it from the AngularJS website. This example was written against v1.6.4.
Copy that file to public/javascripts. Express serves that folder as static files: images, CSS, and JavaScript. The path is set in app.js. Create ng-app.js in the same folder. Then change views/layout.pug as follows:
doctype html
html(ng-app='first-app')
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body(ng-controller='indexController')
block content
script(src='/javascripts/angular.min.js')
script(src='/javascripts/ng-app.js')
The scripts load AngularJS and ng-app.js. The leading / keeps those URLs at the site root. ng-app names the AngularJS module that should bootstrap this page. ng-app.js is:
angular.module('first-app', [])
.controller('indexController', ['$scope', indexController]);
function indexController($scope) {
$scope.name = 'sigfried';
}
This uses AngularJS two-way binding: a change in the controller shows up in the view, and a change in the view updates the controller.
The generated index page also has to show that value:
extends layout
block content
div(ng-controller='indexController')
h1= title
p Welcome {{name}}
input(type='text' ng-model='name')
The input is bound to the name property on the controller's scope:
$scope.name = 'sigfried';
Typing in the input updates the paragraph. {{name}} is AngularJS interpolation.
layout.pug already sets ng-controller='indexController' on body, and this template sets it again on the div. One of them is enough. With both, the inner controller has its own scope.
Run npm start, open http://localhost:3000, and Express serves the page while AngularJS runs in the browser.