node-restify 父路径处理程序
node-restify parent path handler
如果我有两条路径,比如说 /path/one
和 path/two
,我不希望它们都先由父处理程序处理,然后再由它们的特定处理程序处理。我怎样才能实现它。下面的代码将不起作用。他们的特定处理程序从不 运行.
const restify = require('restify');
const app = restify.createServer();
app.get('/path/:type', function (req, res, next) {
console.log(req.params.type + ' handled by parent handler');
next();
});
app.get('/path/one', function (req, res) {
console.log('one handler');
res.end();
});
app.get('/path/two', function (req, res) {
console.log('two handler');
res.end();
});
app.listen(80, function () {
console.log('Server running');
});
不幸的是,这种 "fall through routing" 在 restify 中不受支持。执行与请求匹配的第一个路由处理程序。但是你有一些替代方案来实现给定的用例
已命名 next
调用
您调用 next(req.params.type)
来调用路由 one
或 two
,而不是在没有参数的情况下调用 next()
。注意事项:如果没有为类型注册路由,restify 将发送 500 响应。
const restify = require('restify');
const app = restify.createServer();
app.get('/path/:type', function (req, res, next) {
console.log(req.params.type + ' handled by parent handler');
next(req.params.type);
});
app.get({ name: 'one', path: '/path/one' }, function (req, res) {
console.log('one handler');
res.end();
});
app.get({ name: 'two', path: '/path/two' }, function (req, res) {
console.log('two handler');
res.end();
});
app.listen(80, function () {
console.log('Server running');
});
通用处理程序(express 中的中间件)
由于 restify 没有像 express 那样的挂载功能,我们是否需要手动从当前路由中提取 type
参数:
const restify = require('restify');
const app = restify.createServer();
app.use(function (req, res, next) {
if (req.method == 'GET' && req.route && req.route.path.indexOf('/path') == 0) {
var type = req.route.path.split('/')[2];
console.log(type + ' handled by parent handler');
}
next();
});
app.get('/path/one', function (req, res) {
console.log('one handler');
res.end();
});
app.get('/path/two', function (req, res) {
console.log('two handler');
res.end();
});
app.listen(80, function () {
console.log('Server running');
});
如果我有两条路径,比如说 /path/one
和 path/two
,我不希望它们都先由父处理程序处理,然后再由它们的特定处理程序处理。我怎样才能实现它。下面的代码将不起作用。他们的特定处理程序从不 运行.
const restify = require('restify');
const app = restify.createServer();
app.get('/path/:type', function (req, res, next) {
console.log(req.params.type + ' handled by parent handler');
next();
});
app.get('/path/one', function (req, res) {
console.log('one handler');
res.end();
});
app.get('/path/two', function (req, res) {
console.log('two handler');
res.end();
});
app.listen(80, function () {
console.log('Server running');
});
不幸的是,这种 "fall through routing" 在 restify 中不受支持。执行与请求匹配的第一个路由处理程序。但是你有一些替代方案来实现给定的用例
已命名 next
调用
您调用 next(req.params.type)
来调用路由 one
或 two
,而不是在没有参数的情况下调用 next()
。注意事项:如果没有为类型注册路由,restify 将发送 500 响应。
const restify = require('restify');
const app = restify.createServer();
app.get('/path/:type', function (req, res, next) {
console.log(req.params.type + ' handled by parent handler');
next(req.params.type);
});
app.get({ name: 'one', path: '/path/one' }, function (req, res) {
console.log('one handler');
res.end();
});
app.get({ name: 'two', path: '/path/two' }, function (req, res) {
console.log('two handler');
res.end();
});
app.listen(80, function () {
console.log('Server running');
});
通用处理程序(express 中的中间件)
由于 restify 没有像 express 那样的挂载功能,我们是否需要手动从当前路由中提取 type
参数:
const restify = require('restify');
const app = restify.createServer();
app.use(function (req, res, next) {
if (req.method == 'GET' && req.route && req.route.path.indexOf('/path') == 0) {
var type = req.route.path.split('/')[2];
console.log(type + ' handled by parent handler');
}
next();
});
app.get('/path/one', function (req, res) {
console.log('one handler');
res.end();
});
app.get('/path/two', function (req, res) {
console.log('two handler');
res.end();
});
app.listen(80, function () {
console.log('Server running');
});