Express4 Flashcards

1
Q

Create an Express application (bare bones)

A
var express = require("express");
var app = express();

// routes

app.listen(5000);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Configuration settings (9 of them, names and brief description of what they do)

A
  • “env” Environment mode, defaults to process.env.NODE_ENV (NODE_ENV environment variable) or “development”
  • “trust proxy” Enables reverse proxy support, disabled by default
  • “jsonp callback name” Changes the default callback name of ?callback=
  • “json replacer” JSON replacer callback, null by default
  • “case sensitive routing” Enable case sensitivity, disabled by default, treating “/Foo” and “/foo” as the same
  • “strict routing” Enable strict routing, by default “/foo” and “/foo/” are treated the same by the router
  • “view cache” Enables view template compilation caching, enabled in production by default
  • “view engine” The default engine extension to use when omitted
  • “views” The view directory path, defaulting to “process.cwd() + ‘/views’”
  • “x-powered-by” Enables the X-Powered-By: Express HTTP header, enabled by default
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Set a configuration

A

app.set(name, value);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Get a configuration. What is returned when not yet set?

A

app.get(name);

Returns undefined if not set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Set configuration to true, set to false

A

app. enable(name);

app. disable(name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Check if configuration value is true, or false

A

app. enabled(name);

app. disabled(name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is middleware likened to? What are important considerations?

A

FINISH FROM ROUTES SECTION

Talk about “pipe line”, “sequentially run”, etc.

Middleware is run sequentially, so order of middleware is crucial.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Add middleware to application. What is the optional argument and what is it’s default?

A
app.use(function (req, res, next) {
    // middleware statements goes here
});

Default argument (which comes first if present) is the “mount path”, which defaults to root “/”.

app.use("/admins", function (req, res, next) {
    // login requirement
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Server static files

A

app.use( express.static( __dirname + “/path/to/public” ) );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can middleware functions see what path they were mounted to? Why?

What happens to req.url in this situation?

A

No. Because it makes your apps agnostic of where they are mounted to, that way they can be used more independently and don’t require code changes when code outside of the middleware changes.

req.url has the mounted prefix stripped from it for just this mounted middleware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Register a template engine to handle certain file extensions. Change the default template engine. What does this do?

A

// register all .jade files to use Jade template engine
app.engine(“jade”, require(“jade”).__express);
// change default template engine
app.set(“view engine”, “jade”);

// Now when you call the following, it will assume .jade
app.render("file-name");

// Render HTML files using jade

app. engine(“html”, require(“jade”).__express);
app. set(“view engine”, “html”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Middleware for dynamic route parameters. Different argument types?

Is the parameter middleware always run?

A
// middleware called when route has :name
app.param("name", function (req, res, next, name) {
    // do something with name, then call next();
});
// you can also use regex to validate parameters
app.param("name", /^[A-Z]+$/);

No, it is only run when the a route is called that uses the parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Routing methods (HTTP Methods). Argument options. Dynamic path elements, option path elements, etc.

A

app.get(“/route”, function (req, res) { … });
// methods for all http methods, including:
app.post
app.put
app.del – More proper to use del instead of delete
// method-agnostic
app.all

// dynamic and option path elements
app.get("/path/:dynamic/:optional?", ... );
// You can also use regex as the path
app.get(/^\/commits\/(\w+)(?:\.\.(\w+))?$/, function ... );
// you can also pass in multiple callbacks as arguments
app.get("/admins", user.loadMiddleware, function ...);
// or you can use .all() to provide common callbacks for all method types on that path
app.route("/admins")
    .all(user.loadMiddleware)
    .get(function ... )
    .del(function ... );

// you can also use * to have global matching, which can be super useful, like:

app. all(“/admin/”, authenticateMiddleware);
app. patch(“
”, badMethodError);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Method to avoid having duplicate route-path strings

A

app.route() returns an instance of a single route, so you can do this:

app.route("/path")
    .all( loadDependencies )
    .get(function ... )
    .post(function ... )
    ...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Provide app-level helper methods and variables to all view-templates. What is included by default?

A

app.locals object.

app.locals.title = “App”;
app.locals.formatTime = function (timestamp) {
return …
};

Settings are included by default. So:
app.set("views", "/path/to/views");
app.render("index");
// index.?
{{settings.views}} => /path/to/views
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Application view render

A

app.render(“view-template”, { custom-data }, function (err, html) { … });

17
Q

Object containing properties mapped to the “named route parameters”. Give example. What happens if there aren’t any named route parameters?

A

req.params

/admins/:name => {name}

/admins => { } (empty object if there aren’t any)

18
Q

Adding methods to request or response objects. Best practice and why? How does methods access the actual request or response instance objects?

A

You could have middleware add to the req and res objects, but that would be slower and less memory efficient than adding properties or methods to the request/response prototype objects, like so:

var express = require("express");
var res = express.response;    // response *prototype*
res.method = function() {
    // this method conserves memory since it's a member of prototype
    // this method can access members of a specific request via:
    var locals = this.req.locals;    // notice "this"
    var contentType = this.get("Content-Type");
};

Notice that res.get uses this.get but req.locals uses this.req.locals. The reason is because this is equal to the object you are adding a method to; so if you’re adding a method to the request object, this is already equal to the request object so you can call methods that belong to req directly on this; if you need to access the other side (like referencing the response object while in a request method), you can use this.req/res.

19
Q

Strategies during app loading

A

Loading the application is not like handling every request. The more you do during application loading, the less you need to do for each request. Therefore, only do things during each request that are particular to the request itself. For example, don’t add methods to the response instance, add them to the response prototype, which not only conserves memory but the logic of creating the method is only run when the app is loading.
Similarly, keep “require()” calls and other synchronous calls outside of middleware and position it where it can be called during application loading.