Routing

The app will search for the first matching route in the order you define, so define error routes last

// wiki.js - Wiki route module.
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
  res.send('Wiki home page');
})
module.exports = router;

//in app.js
var wiki = require('./wiki.js');
// ...
app.use('/wiki', wiki);

Router Methods

Methods

  • router.get

  • post, put, delete, options

Callback

Must respond or call next

Post

To access post data as json you need app.use(express.json())

To access post data you need app.use(express.urlencoded())

Routes

Can use regex

Parameters

Static

Now http://localhost:3000/images/kitten.jpg will go to directory of public/images/kitten.jpg

Will search through folders in order declared returning first match

directory from where you launch your nodeprocess. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve:

Error Handling

Just copied from templates

Last updated