forked from sorensen/respectify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute-information.js
55 lines (44 loc) · 1.63 KB
/
route-information.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
// Add an OPTIONS catch all route to display available route information
//
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.2
var assert = require('assert')
, restify = require('restify')
, Respectify = require('../index')
, server = restify.createServer()
, respect = new Respectify(server)
, async = require('async')
// Setup middleware
server.use(restify.queryParser())
server.use(respect.middleware)
// Intercept all OPTIONS method requests, find all specifications for the
// requested route / url for each other HTTP method
server.opts(/.+/, function(req, res, next) {
var _method = req.method
, methods = ['GET', 'POST', 'PUT', 'HEAD', 'DELETE']
// Intended to represent the entire server
if (req.url.replace('/', '') === '*') {
return this.returnRoutes(req, res, next)
}
// Iterate through all HTTP methods to find possible routes
async.mapSeries(methods, function(method, cb) {
// Change the request method so restify can find the correct route
req.method = method
// Find the restify route object
server.router.find(req, res, function(err, route, params) {
if (err && err.statusCode !== 405) return cb(err)
if (!route) return cb(null)
return cb(null, respect.getSpecByRoute(route))
})
}, function(err, resp) {
// Revert to the original method
req.method = _method
// Make sure a valid route was requested
if (err || !resp || !resp.length) {
return next(new restify.ResourceNotFoundError())
}
// Filter out all undefined routes
res.send(200, resp.filter(function(x) { return x }))
})
})
module.exports = server