2017-02-17 131 views
1

我正在學習nodejs/expressjs並將其鎖定到Spring MVC模式,因爲我想我可以保留我的文件的粘性。但它不太工作了,因爲我所期望的......expressjs - 無法處理請求

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; 

後來,當我做一個GET ON /人/我得到的以下控制檯了...

people 
path is /people/ 

我希望app.get運行並得到以下的輸出:

people 
path is /people/ 
people get 
+0

從我所看到的你可以創建一個'app'的新實例,但它不會偵聽任何端口。 'app.get'將匹配請求到它正在監聽的端口。 – char

回答

1

Lurk我強烈推薦使用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 /人,你應該看到節點的console.log。這種方法最大的好處是它可以讓你的代碼保持清潔(無需根據路由切換,你可以讓快速處理寧靜的行爲)

+0

我覺得我比其他方法更喜歡這種方法。你可以將這個路由器/控制器概念應用於'app.method',而不僅僅是中間件? – char

+0

嗨char,不知道我在理解你的問題。這裏的模式是對終點進行分組,例如假設PeopleController具有GET/sayHello和POST/reproduce並且AnimalController具有GET/makeNoise和POST /後代終結點。你可以用app.use('/ people',PeopleController)和app.use('/ animal',AnimalController)'附加'它們。最後用POST /動物/後代消耗它們。如果您問的是按方法對端點進行分組(例如,一個文件中的所有GET調用),那麼這是我不熟悉的一種不同模式。 – Kunal

+0

這是一個乾淨的解決方案。謝謝。 – Lurk21

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 

當你過去的app在server.js

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

和進口在PeopleController.js創造把PeopleController的新實例,你必須

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'); 
    }); 
};