快速路由问题

时间:2018-10-06 15:24:26

标签: node.js express mongoose get routing

我是一个非常新的要表达的人,遇到了建立正确的路由的问题。这是一项家庭作业,因此已经写入了路由器文件,但是每当在该地址处进行get / put / post / delete请求时,我们都应该填写一个express.js文件来调用api。路由器文件的设置如下:

var listings = require('../controllers/listings.server.controller.js'), 
    getCoordinates = require('../controllers/coordinates.server.controller.js'),
    express = require('express'), 
    router = express.Router();

/* 
  These method calls are responsible for routing requests to the correct request handler.
  Take note that it is possible for different controller functions to handle requests to the same route.
 */
router.route('/')
  .get(listings.list)
  .post(getCoordinates, listings.create);

/*
  The ':' specifies a URL parameter. 
 */
router.route('/:listingsId')
  .get(listings.read)
  .put(getCoordinates, listings.update)
  .delete(listings.delete);

/*
  The 'router.param' method allows us to specify middleware we would like to use to handle 
  requests with a parameter.

  Say we make an example request to '/listings/566372f4d11de3498e2941c9'

  The request handler will first find the specific listing using this 'listingsById' 
  middleware function by doing a lookup to ID '566372f4d11de3498e2941c9' in the Mongo database, 
  and bind this listing to the request object.

  It will then pass control to the routing function specified above, where it will either 
  get, update, or delete that specific listing (depending on the HTTP verb specified)
 */
router.param('listingId', listings.listingByID);

module.exports = router;

express.js文件如下:

var path = require('path'),  
    express = require('express'), 
    mongoose = require('mongoose'),
    morgan = require('morgan'),
    bodyParser = require('body-parser'),
    config = require('./config'),
    listingsRouter = require('../routes/listings.server.routes'), 
    getCoordinates = require('../controllers/coordinates.server.controller.js');

module.exports.init = function() {
  //connect to database
  mongoose.connect(config.db.uri, {useMongoClient: true});

  //initialize app
  var app = express();

  //enable request logging for development debugging
  app.use(morgan('dev'));

  //body parsing middleware 
  app.use(bodyParser.json());

  /* server wrapper around Google Maps API to get latitude + longitude coordinates from address */
  app.post('/api/coordinates', getCoordinates, function(req, res) {
    res.send(req.results);
  });

  This is the part I can't figure out:
  /* serve static files */
  app.get('/listings', listingsRouter, function(req, res){
    res.send(req.get('/'))
  });

  /* use the listings router for requests to the api */


  /* go to homepage for all routes not specified */ 

  return app;
};  

我只是不确定如何使用带有req和res对象的listingsRouter文件中的路由,而且找不到这样的程序示例来提供帮助。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

更改以下内容

app.get('/listings', listingsRouter, function(req, res){
    res.send(req.get('/'))
});

收件人

app.use('/listings', listingsRouter);

Express Router。向下滚动到 express.Router 部分以获取完整信息。

希望这个帮助。