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