여기서 다른 해결책 : 당신 기능 조성물을 사용하여 하나의 처리기에 여러 핸들러를 구성 할 수있다. 각 처리기는 req.url
이 일치하지 않으면 다음 처리기로 실행을 생성해야합니다. wrapHandler
은 HTTP 메소드를 고려하여 더 확장 될 수 있습니다.
var http = require('http');
var handler1 = function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('/');
res.end();
}
var handler2 = function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('/Hello');
res.end();
}
var middleware = compose([wrapHandler('/', handler1),
wrapHandler('/hello', handler2)]);
http.createServer(middleware).listen(3000);
function wrapHandler(path, cb) {
return function (req, res, next) {
if (req.url === path) {
cb(req, res);
} else {
next();
}
};
}
function notFoundHandler(req, res) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write('No Path found');
res.end();
};
// adapted from koa-compose
function compose(middleware) {
return function (req, res){
let next = function() {
notFoundHandler.call(this, req, res);
};
let i = middleware.length;
while (i--) {
let thisMiddleware = middleware[i];
let nextMiddleware = next;
next = function() {
thisMiddleware.call(this, req, res, nextMiddleware);
}
}
return next();
}
}
는 해당위한 프레임 워크를 사용, 즉 [espress] (http://expressjs.com/) 또는 [restify] (http://restify.io/) – soulcheck
있습니까'friend'와'foes' URL 경로가 되려고합니까? 또는 쿼리 문자열 매개 변수로 이들의 존재를 테스트하고 싶습니까? –
예, URL 경로입니다. Soulcheck, http://stackoverflow.com/questions/5683916/node-js-express-vs-geddy/5683938#5683938에서 Raynos는 "프레임 워크? 실재 남성은 노드를 직접 사용합니다"라고 말했습니다. 나는 장고가 싫어서 거기에 가고 싶지 않아. – JulienFr