Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | /*! * routelist.js - route list manager * Copyright (c) 2018, The Bcoin Developers (MIT License). * https://github.com/bcoin-org/bcoin */ 'use strict'; const assert = require('bsert'); const Route = require('bweb/lib/route'); // handler for Route const _handler = (req, res) => {}; /** * Route List * @ignore */ class RouteList { /** * Create a route list. * @constructor */ constructor() { this._get = []; this._post = []; this._put = []; this._del = []; } /** * Get lists by methods. * @private * @param {String} method * @returns {RouteItem[]} */ _handlers(method) { assert(typeof method === 'string'); switch (method.toUpperCase()) { case 'GET': return this._get; case 'POST': return this._post; case 'PUT': return this._put; case 'DELETE': return this._del; default: return null; } } /** * check if request matches route in list * @param {Request} req * @param {Response} res * @returns {Boolean} */ has(req) { const routes = this._handlers(req.method); if (!routes) return false; for (const route of routes) { const params = route.match(req.pathname); if (!params) continue; req.params = params; return true; } return false; } /** * Add a GET route. * @param {String} path * @param {Function} handler */ get(path) { this._get.push(new ListItem(path)); } /** * Add a POST route. * @param {String} path * @param {Function} handler */ post(path) { this._post.push(new ListItem(path)); } /** * Add a PUT route. * @param {String} path * @param {Function} handler */ put(path) { this._put.push(new ListItem(path)); } /** * Add a DELETE route. * @param {String} path * @param {Function} handler */ del(path) { this._del.push(new ListItem(path)); } } /** * Route list item * @ignore */ class ListItem extends Route { /** * Create a route list item. * @constructor * @ignore */ constructor(path) { super(path, _handler); } } module.exports = RouteList; |