MCPcopy
hub / github.com/hapijs/hapi / constructor

Method constructor

lib/route.js:27–177  ·  view source on GitHub ↗
(route, server, options = {})

Source from the content-addressed store, hash-verified

25exports = module.exports = internals.Route = class {
26
27 constructor(route, server, options = {}) {
28
29 const core = server._core;
30 const realm = server.realm;
31
32 // Routing information
33
34 Config.apply('route', route, route.method, route.path);
35
36 const method = route.method.toLowerCase();
37 Hoek.assert(method !== 'head', 'Cannot set HEAD route:', route.path);
38
39 const path = realm.modifiers.route.prefix ? realm.modifiers.route.prefix + (route.path !== '/' ? route.path : '') : route.path;
40 Hoek.assert(path === '/' || path[path.length - 1] !== '/' || !core.settings.router.stripTrailingSlash, 'Path cannot end with a trailing slash when configured to strip:', route.method, route.path);
41
42 const vhost = realm.modifiers.route.vhost ?? route.vhost;
43
44 // Set identifying members (assert)
45
46 this.method = method;
47 this.path = path;
48
49 // Prepare configuration
50
51 let config = route.options ?? route.config ?? {};
52 if (typeof config === 'function') {
53 config = config.call(realm.settings.bind, server);
54 }
55
56 config = Config.enable(config); // Shallow clone
57
58 // Verify route level config (as opposed to the merged settings)
59
60 this._assert(method !== 'get' || !config.payload, 'Cannot set payload settings on HEAD or GET request');
61 this._assert(method !== 'get' || !config.validate?.payload, 'Cannot validate HEAD or GET request payload');
62
63 // Rules
64
65 this._assert(!route.rules || !config.rules, 'Route rules can only appear once'); // XOR
66 const rules = route.rules ?? config.rules;
67 const rulesConfig = internals.rules(rules, { method, path, vhost }, server);
68 delete config.rules;
69
70 // Handler
71
72 this._assert(route.handler || config.handler, 'Missing or undefined handler');
73 this._assert(!!route.handler ^ !!config.handler, 'Handler must only appear once'); // XOR
74
75 const handler = Config.apply('handler', route.handler ?? config.handler);
76 delete config.handler;
77
78 const handlerDefaults = Handler.defaults(method, handler, core);
79
80 // Apply settings in order: server <- handler <- realm <- route
81
82 const settings = internals.config([core.settings.routes, handlerDefaults, realm.settings, rulesConfig, config]);
83 this.settings = Config.apply('routeConfig', settings, method, path);
84

Callers

nothing calls this directly

Calls 8

_assertMethod · 0.80
rulesMethod · 0.80
testAccessMethod · 0.80
_setupValidationMethod · 0.80
_setupRouteMethod · 0.80
routeMethod · 0.80
combineMethod · 0.80
rebuildMethod · 0.80

Tested by

no test coverage detected