| 172 | return this |
| 173 | } |
| 174 | use(...args: UseMethodParams<Req, Res, App>) { |
| 175 | const base = args[0] |
| 176 | |
| 177 | const fns = args.slice(1).flat() |
| 178 | |
| 179 | if (base instanceof App) { |
| 180 | // Set App parent to current App |
| 181 | base.parent = this |
| 182 | |
| 183 | // Mount on root |
| 184 | base.mountpath = '/' |
| 185 | |
| 186 | this.apps['/'] = base |
| 187 | } |
| 188 | |
| 189 | const path = typeof base === 'string' ? base : '/' |
| 190 | |
| 191 | let regex: { keys: string[]; pattern: RegExp } |
| 192 | |
| 193 | for (const fn of fns) { |
| 194 | if (fn instanceof App) { |
| 195 | regex = rg(path, true) |
| 196 | |
| 197 | fn.mountpath = path |
| 198 | |
| 199 | this.apps[path] = fn |
| 200 | |
| 201 | fn.parent = this |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (base === '/') { |
| 206 | for (const fn of fns) super.use(base, mount(fn as Handler)) |
| 207 | } else if (typeof base === 'function' || base instanceof App) { |
| 208 | super.use('/', [base, ...fns].map(mount)) |
| 209 | } else if (Array.isArray(base)) { |
| 210 | super.use('/', [...base, ...fns].map(mount)) |
| 211 | } else { |
| 212 | pushMiddleware(this.middleware)({ |
| 213 | path: base as string, |
| 214 | regex, |
| 215 | type: 'mw', |
| 216 | handler: mount(fns[0] as Handler), |
| 217 | handlers: fns.slice(1).map(mount) |
| 218 | }) |
| 219 | } |
| 220 | |
| 221 | return this // chainable |
| 222 | } |
| 223 | /** |
| 224 | * Register a template engine with extension |
| 225 | */ |