expressjs - 无法处理请求

时间:2017-02-17 03:00:27

标签: javascript node.js express

我正在学习nodejs / expressjs并将其加入到Spring MVC模式中,因为我认为我可以保持我的文件的凝聚力。但它并没有像我期望的那样发挥作用......

server.js

const express = require('express');
const bodyParser= require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const app = express();

var PeopleController = require('./controller/PeopleController.js')
var db;

app.all('/*', function(req, res, next) {
  var path = req.url;
  var controller;

  switch (true) {
    case /\/people.*/g.test(path):
      controller = new PeopleController();
      console.log("people");
      controller.process(req, res, next);
      break;
    case /\/foo.*/g.test(path):
      console.log("foo");
      break
    default:
      console.log("nada");
      break;
  }
});

PeopleController.js

const express = require('express');
const bodyParser= require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const app = express();

var PeopleController = function PeopleController() {}

PeopleController.prototype.process = function (req, res, next) {
  var baseURL = '/people';

  console.log('path is ' + req.path);

  app.use(bodyParser.urlencoded({extended: true}));
  app.set('view engine', 'ejs');

  app.get(baseURL, (req, res) => {
    console.log('people get');
  });

  app.post(baseURL, (req, res) => {
    console.log('people post');
  });
};

module.exports = PeopleController;

然后,当我对/ people /我进行GET时,我得到以下控制台...

people
path is /people/

我希望app.get能够运行并获得以下输出:

people
path is /people/
people get

2 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为您只有一个Express实例侦听端口。

在server.js中:

const express = require('express');
const bodyParser= require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const app = express(); // we create an instance of Express
...
app.listen(process.ENV.PORT); //this specific instance listening on port

当您创建PeopleController的新实例时,您必须经过server.js中的app

const app = express();
...
controller = new PeopleController(app);

并在PeopleController.js中导入

const express = require('express'); // delete this
const app = express(); // delete this

//add this:
var PeopleController = function PeopleController(appInstance) {
  this.app = appInstance;
}

PeopleController.prototype.process = function (req, res, next) {
  var baseURL = '/people';
  var app = this.app;

  console.log('path is ' + req.path);

  app.use(bodyParser.urlencoded({extended: true}));
  app.set('view engine', 'ejs');

  app.get(baseURL, (req, res) => {
    console.log('people get');
  });

  app.post(baseURL, (req, res) => {
    console.log('people post');
  });
};

答案 1 :(得分:1)

潜伏我强烈建议您使用express.router

PeopleController.js

const express = require('express');
const router = express.Router();

// then apply middleware
router.use(...);

// then define your endpoints
router.get('/', (req, res) => {
  console.log('get PeopleController.js root');
}

module.exports = router;

在server.js

...
const PeopleController = require('./controller/PeopleController.js');

// apply PeopleController to '/people' route
app.use('/people', PeopleController);

所以现在当你请求GET / people时,你应该在节点上看到console.log。这种方法的最大好处是它可以保持代码清洁(没有基于路由的切换,你可以表达处理休息的动作)