Node.js Express-export-Routen für die Organisation?

Nutzen express.Router() für API-Aufrufe zu/von unserer Anwendung:

var express = require('express');
var app     = express();
var router  = express.Router();

router.use Konsole.Protokolle vor jedem API-Aufruf:

router.use(function(req, res, next) { //run for any & all requests
    console.log("Connection to the API.."); //set up logging for every API call
    next(); //..to the next routes from here..
});

Wie wir exportieren unsere Routen zu folder/routes.js und greifen Sie von unserer app.js, wo Sie sich gerade befinden:

router.route('/This') //on routes for /This
    //post a new This (accessed by POST @ http://localhost:8888/api/v1/This)
    .post(function(req, res) {
        //do stuff
    });

router.route('/That') //on routes for /That
    //post a new That (accessed by POST @ http://localhost:8888/api/v1/That)
    .post(function(req, res) {
        //do stuff
    });

...wenn wir das Präfix, das jede route mit:

app.use('/api/v1', router); //all of the API routes are prefixed with '/api' version '/v1'
InformationsquelleAutor Stacks | 2015-08-29
Schreibe einen Kommentar