Defining routes for the app
From the examples in Chapter 1, What is Express? and Chapter 2, Your First Express App, we know how routes and route handler callback functions look like. Here is an example to refresh your memory:
app.get('/', function(req, res) { res.send('welcome'); });
Routes in Express are created using methods named after HTTP verbs. For instance, in the previous example, we created a route to handle GET
requests to the root of the website. You have a corresponding method on the app
object for all the HTTP verbs listed earlier.
Let's create a sample application to see if all the HTTP verbs are actually available as methods in the app
object:
var http = require('http'); var express = require('express'); var app = express(); // Include the router middleware app.use(app.router); // GET request to the root URL app.get('/', function(req, res) { res.send('/ GET OK'); }); // POST request to the root URL app.post('/', function(req, res) { res.send('/ POST OK'); }); // PUT...