首页 > 代码库 > [Express] Level 5: Route file

[Express] Level 5: Route file

Using a Router Instance

Let‘s refactor app.js to use a Router object.

Create a new router object and assign it to the router variable.

var router = express.Router();

When we are done, our router will be mounted on the /cities path. With this in mind, change app.route(‘/cities‘) to use router and map requests to the root path.

app.route(‘/cities‘)  .get(function (request, response) {    if(request.query.search){      response.json(citySearch(request.query.search));    }else{      response.json(cities);    }  })//torouter.route(‘/‘)  .get(function (request, response) {    if(request.query.search){      response.json(citySearch(request.query.search));    }else{      response.json(cities);    }  })

Likewise, let‘s move our ‘/cities/:name‘ route to our router. Remember to update the path.

app.route(‘/cities/:name‘)  .get(function (request, response) {    var cityInfo = cities[request.cityName];    if(cityInfo){      response.json(cityInfo);    }else{      response.status(404).json("City not found");    }  })  .delete(function (request, response) {    if(cities[request.cityName]){      delete cities[request.cityName];      response.sendStatus(200);    }else{      response.sendStatus(404);    }  });//torouter.route(‘/:name‘)  .get(function (request, response) {    var cityInfo = cities[request.cityName];    if(cityInfo){      response.json(cityInfo);    }else{      response.status(404).json("City not found");    }  })  .delete(function (request, response) {    if(cities[request.cityName]){      delete cities[request.cityName];      response.sendStatus(200);    }else{      response.sendStatus(404);    }  });

Our router is now ready to be used by app. Mount our new router under the /cities path.

app.use(‘/cities‘, router);

 

var express = require(‘express‘);var app = express();var bodyParser = require(‘body-parser‘);var parseUrlencoded = bodyParser.urlencoded({ extended: false });// In memory store for the// cities in our applicationvar cities = {  ‘Lotopia‘: ‘Rough and mountainous‘,  ‘Caspiana‘: ‘Sky-top island‘,  ‘Indigo‘: ‘Vibrant and thriving‘,  ‘Paradise‘: ‘Lush, green plantation‘,  ‘Flotilla‘: ‘Bustling urban oasis‘};app.param(‘name‘, function (request, response, next) {  request.cityName = parseCityName(request.params.name);});var router = express.Router();app.use(‘/cities‘, router);router.route(‘/‘)  .get(function (request, response) {    if(request.query.search){      response.json(citySearch(request.query.search));    }else{      response.json(cities);    }  })  .post(parseUrlencoded, function (request, response) {    if(request.body.description.length > 4){      var city = createCity(request.body.name, request.body.description);      response.status(201).json(city);    }else{      response.status(400).json(‘Invalid City‘);    }  });router.route(‘/:name‘)  .get(function (request, response) {    var cityInfo = cities[request.cityName];    if(cityInfo){      response.json(cityInfo);    }else{      response.status(404).json("City not found");    }  })  .delete(function (request, response) {    if(cities[request.cityName]){      delete cities[request.cityName];      response.sendStatus(200);    }else{      response.sendStatus(404);    }  });// Searches for keyword in description// and returns the cityfunction citySearch(keyword) {  var regexp = RegExp(keyword, ‘i‘);  var result = cities.filter(function (city) {    return city.match(regexp);  });  return result;}// Adds a new city to the// in memory storefunction createCity(name, description){  cities[name] = description;  return name;}// Uppercase the city name.function parseCityName(name){  var parsedName = name[0].toUpperCase() + name.slice(1).toLowerCase();  return parsedName;}app.listen(3000);

 

All HTTP Verbs

What function would you call to match all HTTP verbs?

Answer:

app.all();

 

Using All

Let‘s use the app.all() method to handle the name parameter instead of app.param().

Add a call to all() for our router‘s ‘/:name‘ route. Pass a callback function that accepts requestresponse, and next.

router.route(‘/:name‘)    .all(function(request, response, next){    })

Now let‘s take our logic from the callback function passed to app.param()and move it to our all() callback.

router.route(‘/:name‘)    .all(function(request, response, next){    request.cityName = parseCityName(request.params.name);  })

 

var express = require(‘express‘);var app = express();var bodyParser = require(‘body-parser‘);var parseUrlencoded = bodyParser.urlencoded({ extended: false });// In memory store for the cities in our applicationvar cities = {  ‘Lotopia‘: ‘Rough and mountainous‘,  ‘Caspiana‘: ‘Sky-top island‘,  ‘Indigo‘: ‘Vibrant and thriving‘,  ‘Paradise‘: ‘Lush, green plantation‘,  ‘Flotilla‘: ‘Bustling urban oasis‘};// Searches for keyword in description and returns the cityfunction citySearch(keyword) {  var regexp = RegExp(keyword, ‘i‘);  var result = cities.filter(function (city) {    return city.match(regexp);  });  return result;}// Adds a new city to the in memory storefunction createCity(name, description) {  cities[name] = description;  return name;}// Uppercase the city name.function parseCityName(name) {  var parsedName = name[0].toUpperCase() + name.slice(1).toLowerCase();  return parsedName;}var router = express.Router();router.route(‘/‘)  .get(function (request, response) {    if(request.query.search) {      response.json(citySearch(request.query.search));    } else {      response.json(cities);    }  })  .post(parseUrlencoded, function (request, response) {    if(request.body.description.length > 4) {      var city = createCity(request.body.name, request.body.description);      response.status(201).json(city);    } else {      response.status(400).json(‘Invalid City‘);    }  });router.route(‘/:name‘)    .all(function(request, response, next){    request.cityName = parseCityName(request.params.name);  })  .get(function (request, response) {    var cityInfo = cities[request.cityName];    if(cityInfo) {      response.json(cityInfo);    } else {      response.status(404).json("City not found");    }  })  .delete(function (request, response) {    if(cities[request.cityName]) {      delete cities[request.cityName];      response.sendStatus(200);    } else {      response.sendStatus(404);    }  });app.use(‘/cities‘, router);app.listen(3000);

 

Creating a Router Module

Our single application file is growing too long. It‘s time we extract our routes to a separate Node module under the routes folder.

Move our router and its supporting code from app.js toroutes/cities.js.

routes/cities.js

var bodyParser = require(‘body-parser‘);var parseUrlencoded = bodyParser.urlencoded({ extended: false });// In memory store for the// cities in our applicationvar cities = {  ‘Lotopia‘: ‘Rough and mountainous‘,  ‘Caspiana‘: ‘Sky-top island‘,  ‘Indigo‘: ‘Vibrant and thriving‘,  ‘Paradise‘: ‘Lush, green plantation‘,  ‘Flotilla‘: ‘Bustling urban oasis‘};var router = express.Router();router.route(‘/‘)  .get(function (request, response) {    if(request.query.search){      response.json(citySearch(request.query.search));    }else{      response.json(cities);    }  })  .post(parseUrlencoded, function (request, response) {    if(request.body.description.length > 4){      var city = createCity(request.body.name, request.body.description);      response.status(201).json(city);    }else{      response.status(400).json(‘Invalid City‘);    }  });router.route(‘/:name‘)  .all(function (request, response, next) {    request.cityName = parseCityName(request.params.name);  })  .get(function (request, response) {    var cityInfo = cities[request.cityName];    if(cityInfo){      response.json(cityInfo);    }else{      response.status(404).json("City not found");    }  })  .delete(function (request, response) {    if(cities[request.cityName]){      delete cities[request.cityName];      response.sendStatus(200);    }else{      response.sendStatus(404);    }  });// Searches for keyword in description// and returns the cityfunction citySearch(keyword) {  var regexp = RegExp(keyword, ‘i‘);  var result = cities.filter(function (city) {    return city.match(regexp);  });  return result;}// Adds a new city to the// in memory storefunction createCity(name, description){  cities[name] = description;  return name;}// Uppercase the city name.function parseCityName(name){  var parsedName = name[0].toUpperCase() + name.slice(1).toLowerCase();  return parsedName;}

export our router object so other files can have access to it. Remember, Node - therefore Express - uses the CommonJS module specification.

module.exports = router;

Our cities routes module is now ready to be used from app.js. Require the new routes/cities module from app.js and assign it to a variable calledrouter;

app.js

var router = require(‘./routes/cities‘);

 

app.js

var express = require(‘express‘);var app = express();var router = require(‘./routes/cities‘);app.use(‘/cities‘, router);app.listen(3000);

routes/cities.js

var express = require(‘express‘);var bodyParser = require(‘body-parser‘);var parseUrlencoded = bodyParser.urlencoded({ extended: false });// In memory store for the// cities in our applicationvar cities = {  ‘Lotopia‘: ‘Rough and mountainous‘,  ‘Caspiana‘: ‘Sky-top island‘,  ‘Indigo‘: ‘Vibrant and thriving‘,  ‘Paradise‘: ‘Lush, green plantation‘,  ‘Flotilla‘: ‘Bustling urban oasis‘};var router = express.Router();router.route(‘/‘)  .get(function (request, response) {    if(request.query.search){      response.json(citySearch(request.query.search));    }else{      response.json(cities);    }  })  .post(parseUrlencoded, function (request, response) {    if(request.body.description.length > 4){      var city = createCity(request.body.name, request.body.description);      response.status(201).json(city);    }else{      response.status(400).json(‘Invalid City‘);    }  });router.route(‘/:name‘)  .all(function (request, response, next) {    request.cityName = parseCityName(request.params.name);  })  .get(function (request, response) {    var cityInfo = cities[request.cityName];    if(cityInfo){      response.json(cityInfo);    }else{      response.status(404).json("City not found");    }  })  .delete(function (request, response) {    if(cities[request.cityName]){      delete cities[request.cityName];      response.sendStatus(200);    }else{      response.sendStatus(404);    }  });// Searches for keyword in description// and returns the cityfunction citySearch(keyword) {  var regexp = RegExp(keyword, ‘i‘);  var result = cities.filter(function (city) {    return city.match(regexp);  });  return result;}// Adds a new city to the// in memory storefunction createCity(name, description){  cities[name] = description;  return name;}// Uppercase the city name.function parseCityName(name){  var parsedName = name[0].toUpperCase() + name.slice(1).toLowerCase();  return parsedName;}module.exports = router;

 

[Express] Level 5: Route file