Javascript expressjs-无法处理请求
我正在学习nodejs/expressjs,并将其嵌入到springmvc模式中,因为我认为我可以保持文件的内聚性。但这并不像我想象的那样有效 server.jsJavascript expressjs-无法处理请求,javascript,node.js,express,Javascript,Node.js,Express,我正在学习nodejs/expressjs,并将其嵌入到springmvc模式中,因为我认为我可以保持文件的内聚性。但这并不像我想象的那样有效 server.js const express = require('express'); const bodyParser= require('body-parser'); const MongoClient = require('mongodb').MongoClient; const app = express(); var PeopleCont
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;
}
});
const app = express();
...
controller = new PeopleController(app);
...
const PeopleController = require('./controller/PeopleController.js');
// apply PeopleController to '/people' route
app.use('/people', PeopleController);
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;
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');
});
};
然后,当我做一个GET-on/people/I时,我会拿出下面的控制台
people
path is /people/
我希望app.get运行并获得以下输出:
people
path is /people/
people get
发生这种情况是因为在端口上只有一个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中的应用程序
const app = express();
...
controller = new PeopleController(app);
...
const PeopleController = require('./controller/PeopleController.js');
// apply PeopleController to '/people' route
app.use('/people', PeopleController);
并在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;
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');
});
};
潜伏我强烈建议使用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 app = express();
...
controller = new PeopleController(app);
...
const PeopleController = require('./controller/PeopleController.js');
// apply PeopleController to '/people' route
app.use('/people', PeopleController);
因此,现在当您请求GET/people时,您应该在节点上看到console.log。这种方法的最大好处是它保持了代码的整洁(没有基于路由的切换,您可以让express处理restful操作)从我看到的情况来看,您创建了一个app
的新实例,但它不监听任何端口app.get
将请求与它正在侦听的端口相匹配。我想我更喜欢这种方法。您能否将此路由器/控制器概念应用于app.method
,而不仅仅应用于中间件?嗨,char,我不确定我是否理解您的问题。这里的模式是对端点进行分组,例如,假设PeopleController有GET/sayHello和POST/reproduction,AnimalController有GET/makeNoise和POST/founders端点。您可以使用app.use('/people',PeopleController)和app.use('/AnimalController',AnimalController)来“附加”它们。最后将其与POST/动物/后代一起食用。如果您询问按方法对端点进行分组(例如,在一个文件中所有GET调用),那么这是一种我不熟悉的不同模式。这是一个干净的解决方案。谢谢,干得好。实际上,我最初是以与您类似的方式构建api的,直到github上的一个好家伙告诉我更好!