説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

express.d.ts 37KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. // Type definitions for Express 4.x
  2. // Project: http://expressjs.com
  3. // Definitions by: Boris Yankov <https://github.com/borisyankov/>
  4. // Definitions: https://github.com/borisyankov/DefinitelyTyped
  5. /* =================== USAGE ===================
  6. import * as express from "express"
  7. var app = express();
  8. =============================================== */
  9. /// <reference path="../node/node.d.ts" />
  10. /// <reference path="../serve-static/serve-static.d.ts" />
  11. declare module Express {
  12. // These open interfaces may be extended in an application-specific manner via declaration merging.
  13. // See for example method-override.d.ts (https://github.com/borisyankov/DefinitelyTyped/blob/master/method-override/method-override.d.ts)
  14. export interface Request { }
  15. export interface Response { }
  16. export interface Application { }
  17. }
  18. declare module "express" {
  19. import * as http from "http"
  20. import * as serveStatic from "serve-static";
  21. function e(): e.Express;
  22. module e {
  23. interface IRoute {
  24. path: string;
  25. stack: any;
  26. all(...handler: RequestHandler[]): IRoute;
  27. get(...handler: RequestHandler[]): IRoute;
  28. post(...handler: RequestHandler[]): IRoute;
  29. put(...handler: RequestHandler[]): IRoute;
  30. delete(...handler: RequestHandler[]): IRoute;
  31. patch(...handler: RequestHandler[]): IRoute;
  32. options(...handler: RequestHandler[]): IRoute;
  33. }
  34. interface IRouterMatcher<T> {
  35. (name: string, ...handlers: RequestHandler[]): T;
  36. (name: RegExp, ...handlers: RequestHandler[]): T;
  37. }
  38. interface IRouter<T> extends RequestHandler {
  39. /**
  40. * Map the given param placeholder `name`(s) to the given callback(s).
  41. *
  42. * Parameter mapping is used to provide pre-conditions to routes
  43. * which use normalized placeholders. For example a _:user_id_ parameter
  44. * could automatically load a user's information from the database without
  45. * any additional code,
  46. *
  47. * The callback uses the samesignature as middleware, the only differencing
  48. * being that the value of the placeholder is passed, in this case the _id_
  49. * of the user. Once the `next()` function is invoked, just like middleware
  50. * it will continue on to execute the route, or subsequent parameter functions.
  51. *
  52. * app.param('user_id', function(req, res, next, id){
  53. * User.find(id, function(err, user){
  54. * if (err) {
  55. * next(err);
  56. * } else if (user) {
  57. * req.user = user;
  58. * next();
  59. * } else {
  60. * next(new Error('failed to load user'));
  61. * }
  62. * });
  63. * });
  64. *
  65. * @param name
  66. * @param fn
  67. */
  68. param(name: string, handler: RequestParamHandler): T;
  69. param(name: string, matcher: RegExp): T;
  70. param(name: string, mapper: (param: any) => any): T;
  71. // Alternatively, you can pass only a callback, in which case you have the opportunity to alter the app.param() API
  72. param(callback: (name: string, matcher: RegExp) => RequestParamHandler): T;
  73. /**
  74. * Special-cased "all" method, applying the given route `path`,
  75. * middleware, and callback to _every_ HTTP method.
  76. *
  77. * @param path
  78. * @param fn
  79. */
  80. all: IRouterMatcher<T>;
  81. get: IRouterMatcher<T>;
  82. post: IRouterMatcher<T>;
  83. put: IRouterMatcher<T>;
  84. delete: IRouterMatcher<T>;
  85. patch: IRouterMatcher<T>;
  86. options: IRouterMatcher<T>;
  87. route(path: string): IRoute;
  88. use(...handler: RequestHandler[]): T;
  89. use(handler: ErrorRequestHandler): T;
  90. use(path: string, ...handler: RequestHandler[]): T;
  91. use(path: string, handler: ErrorRequestHandler): T;
  92. }
  93. export function Router(options?: any): Router;
  94. export interface Router extends IRouter<Router> {}
  95. interface CookieOptions {
  96. maxAge?: number;
  97. signed?: boolean;
  98. expires?: Date;
  99. httpOnly?: boolean;
  100. path?: string;
  101. domain?: string;
  102. secure?: boolean;
  103. }
  104. interface Errback { (err: Error): void; }
  105. interface Request extends http.ServerRequest, Express.Request {
  106. /**
  107. * Return request header.
  108. *
  109. * The `Referrer` header field is special-cased,
  110. * both `Referrer` and `Referer` are interchangeable.
  111. *
  112. * Examples:
  113. *
  114. * req.get('Content-Type');
  115. * // => "text/plain"
  116. *
  117. * req.get('content-type');
  118. * // => "text/plain"
  119. *
  120. * req.get('Something');
  121. * // => undefined
  122. *
  123. * Aliased as `req.header()`.
  124. *
  125. * @param name
  126. */
  127. get (name: string): string;
  128. header(name: string): string;
  129. headers: { [key: string]: string; };
  130. /**
  131. * Check if the given `type(s)` is acceptable, returning
  132. * the best match when true, otherwise `undefined`, in which
  133. * case you should respond with 406 "Not Acceptable".
  134. *
  135. * The `type` value may be a single mime type string
  136. * such as "application/json", the extension name
  137. * such as "json", a comma-delimted list such as "json, html, text/plain",
  138. * or an array `["json", "html", "text/plain"]`. When a list
  139. * or array is given the _best_ match, if any is returned.
  140. *
  141. * Examples:
  142. *
  143. * // Accept: text/html
  144. * req.accepts('html');
  145. * // => "html"
  146. *
  147. * // Accept: text/*, application/json
  148. * req.accepts('html');
  149. * // => "html"
  150. * req.accepts('text/html');
  151. * // => "text/html"
  152. * req.accepts('json, text');
  153. * // => "json"
  154. * req.accepts('application/json');
  155. * // => "application/json"
  156. *
  157. * // Accept: text/*, application/json
  158. * req.accepts('image/png');
  159. * req.accepts('png');
  160. * // => undefined
  161. *
  162. * // Accept: text/*;q=.5, application/json
  163. * req.accepts(['html', 'json']);
  164. * req.accepts('html, json');
  165. * // => "json"
  166. */
  167. accepts(type: string): string;
  168. accepts(type: string[]): string;
  169. /**
  170. * Check if the given `charset` is acceptable,
  171. * otherwise you should respond with 406 "Not Acceptable".
  172. *
  173. * @param charset
  174. */
  175. acceptsCharset(charset: string): boolean;
  176. /**
  177. * Check if the given `lang` is acceptable,
  178. * otherwise you should respond with 406 "Not Acceptable".
  179. *
  180. * @param lang
  181. */
  182. acceptsLanguage(lang: string): boolean;
  183. /**
  184. * Parse Range header field,
  185. * capping to the given `size`.
  186. *
  187. * Unspecified ranges such as "0-" require
  188. * knowledge of your resource length. In
  189. * the case of a byte range this is of course
  190. * the total number of bytes. If the Range
  191. * header field is not given `null` is returned,
  192. * `-1` when unsatisfiable, `-2` when syntactically invalid.
  193. *
  194. * NOTE: remember that ranges are inclusive, so
  195. * for example "Range: users=0-3" should respond
  196. * with 4 users when available, not 3.
  197. *
  198. * @param size
  199. */
  200. range(size: number): any[];
  201. /**
  202. * Return an array of Accepted media types
  203. * ordered from highest quality to lowest.
  204. */
  205. accepted: MediaType[];
  206. /**
  207. * Return an array of Accepted languages
  208. * ordered from highest quality to lowest.
  209. *
  210. * Examples:
  211. *
  212. * Accept-Language: en;q=.5, en-us
  213. * ['en-us', 'en']
  214. */
  215. acceptedLanguages: any[];
  216. /**
  217. * Return an array of Accepted charsets
  218. * ordered from highest quality to lowest.
  219. *
  220. * Examples:
  221. *
  222. * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8
  223. * ['unicode-1-1', 'iso-8859-5']
  224. */
  225. acceptedCharsets: any[];
  226. /**
  227. * Return the value of param `name` when present or `defaultValue`.
  228. *
  229. * - Checks route placeholders, ex: _/user/:id_
  230. * - Checks body params, ex: id=12, {"id":12}
  231. * - Checks query string params, ex: ?id=12
  232. *
  233. * To utilize request bodies, `req.body`
  234. * should be an object. This can be done by using
  235. * the `connect.bodyParser()` middleware.
  236. *
  237. * @param name
  238. * @param defaultValue
  239. */
  240. param(name: string, defaultValue?: any): string;
  241. /**
  242. * Check if the incoming request contains the "Content-Type"
  243. * header field, and it contains the give mime `type`.
  244. *
  245. * Examples:
  246. *
  247. * // With Content-Type: text/html; charset=utf-8
  248. * req.is('html');
  249. * req.is('text/html');
  250. * req.is('text/*');
  251. * // => true
  252. *
  253. * // When Content-Type is application/json
  254. * req.is('json');
  255. * req.is('application/json');
  256. * req.is('application/*');
  257. * // => true
  258. *
  259. * req.is('html');
  260. * // => false
  261. *
  262. * @param type
  263. */
  264. is(type: string): boolean;
  265. /**
  266. * Return the protocol string "http" or "https"
  267. * when requested with TLS. When the "trust proxy"
  268. * setting is enabled the "X-Forwarded-Proto" header
  269. * field will be trusted. If you're running behind
  270. * a reverse proxy that supplies https for you this
  271. * may be enabled.
  272. */
  273. protocol: string;
  274. /**
  275. * Short-hand for:
  276. *
  277. * req.protocol == 'https'
  278. */
  279. secure: boolean;
  280. /**
  281. * Return the remote address, or when
  282. * "trust proxy" is `true` return
  283. * the upstream addr.
  284. */
  285. ip: string;
  286. /**
  287. * When "trust proxy" is `true`, parse
  288. * the "X-Forwarded-For" ip address list.
  289. *
  290. * For example if the value were "client, proxy1, proxy2"
  291. * you would receive the array `["client", "proxy1", "proxy2"]`
  292. * where "proxy2" is the furthest down-stream.
  293. */
  294. ips: string[];
  295. /**
  296. * Return subdomains as an array.
  297. *
  298. * Subdomains are the dot-separated parts of the host before the main domain of
  299. * the app. By default, the domain of the app is assumed to be the last two
  300. * parts of the host. This can be changed by setting "subdomain offset".
  301. *
  302. * For example, if the domain is "tobi.ferrets.example.com":
  303. * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.
  304. * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.
  305. */
  306. subdomains: string[];
  307. /**
  308. * Short-hand for `url.parse(req.url).pathname`.
  309. */
  310. path: string;
  311. /**
  312. * Parse the "Host" header field hostname.
  313. */
  314. hostname: string;
  315. /**
  316. * @deprecated Use hostname instead.
  317. */
  318. host: string;
  319. /**
  320. * Check if the request is fresh, aka
  321. * Last-Modified and/or the ETag
  322. * still match.
  323. */
  324. fresh: boolean;
  325. /**
  326. * Check if the request is stale, aka
  327. * "Last-Modified" and / or the "ETag" for the
  328. * resource has changed.
  329. */
  330. stale: boolean;
  331. /**
  332. * Check if the request was an _XMLHttpRequest_.
  333. */
  334. xhr: boolean;
  335. //body: { username: string; password: string; remember: boolean; title: string; };
  336. body: any;
  337. //cookies: { string; remember: boolean; };
  338. cookies: any;
  339. method: string;
  340. params: any;
  341. user: any;
  342. authenticatedUser: any;
  343. files: any;
  344. /**
  345. * Clear cookie `name`.
  346. *
  347. * @param name
  348. * @param options
  349. */
  350. clearCookie(name: string, options?: any): Response;
  351. query: any;
  352. route: any;
  353. signedCookies: any;
  354. originalUrl: string;
  355. url: string;
  356. }
  357. interface MediaType {
  358. value: string;
  359. quality: number;
  360. type: string;
  361. subtype: string;
  362. }
  363. interface Send {
  364. (status: number, body?: any): Response;
  365. (body: any): Response;
  366. }
  367. interface Response extends http.ServerResponse, Express.Response {
  368. /**
  369. * Set status `code`.
  370. *
  371. * @param code
  372. */
  373. status(code: number): Response;
  374. /**
  375. * Set the response HTTP status code to `statusCode` and send its string representation as the response body.
  376. * @link http://expressjs.com/4x/api.html#res.sendStatus
  377. *
  378. * Examples:
  379. *
  380. * res.sendStatus(200); // equivalent to res.status(200).send('OK')
  381. * res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')
  382. * res.sendStatus(404); // equivalent to res.status(404).send('Not Found')
  383. * res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')
  384. *
  385. * @param code
  386. */
  387. sendStatus(code: number): Response;
  388. /**
  389. * Set Link header field with the given `links`.
  390. *
  391. * Examples:
  392. *
  393. * res.links({
  394. * next: 'http://api.example.com/users?page=2',
  395. * last: 'http://api.example.com/users?page=5'
  396. * });
  397. *
  398. * @param links
  399. */
  400. links(links: any): Response;
  401. /**
  402. * Send a response.
  403. *
  404. * Examples:
  405. *
  406. * res.send(new Buffer('wahoo'));
  407. * res.send({ some: 'json' });
  408. * res.send('<p>some html</p>');
  409. * res.send(404, 'Sorry, cant find that');
  410. * res.send(404);
  411. */
  412. send: Send;
  413. /**
  414. * Send JSON response.
  415. *
  416. * Examples:
  417. *
  418. * res.json(null);
  419. * res.json({ user: 'tj' });
  420. * res.json(500, 'oh noes!');
  421. * res.json(404, 'I dont have that');
  422. */
  423. json: Send;
  424. /**
  425. * Send JSON response with JSONP callback support.
  426. *
  427. * Examples:
  428. *
  429. * res.jsonp(null);
  430. * res.jsonp({ user: 'tj' });
  431. * res.jsonp(500, 'oh noes!');
  432. * res.jsonp(404, 'I dont have that');
  433. */
  434. jsonp: Send;
  435. /**
  436. * Transfer the file at the given `path`.
  437. *
  438. * Automatically sets the _Content-Type_ response header field.
  439. * The callback `fn(err)` is invoked when the transfer is complete
  440. * or when an error occurs. Be sure to check `res.sentHeader`
  441. * if you wish to attempt responding, as the header and some data
  442. * may have already been transferred.
  443. *
  444. * Options:
  445. *
  446. * - `maxAge` defaulting to 0 (can be string converted by `ms`)
  447. * - `root` root directory for relative filenames
  448. * - `headers` object of headers to serve with file
  449. * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them
  450. *
  451. * Other options are passed along to `send`.
  452. *
  453. * Examples:
  454. *
  455. * The following example illustrates how `res.sendFile()` may
  456. * be used as an alternative for the `static()` middleware for
  457. * dynamic situations. The code backing `res.sendFile()` is actually
  458. * the same code, so HTTP cache support etc is identical.
  459. *
  460. * app.get('/user/:uid/photos/:file', function(req, res){
  461. * var uid = req.params.uid
  462. * , file = req.params.file;
  463. *
  464. * req.user.mayViewFilesFrom(uid, function(yes){
  465. * if (yes) {
  466. * res.sendFile('/uploads/' + uid + '/' + file);
  467. * } else {
  468. * res.send(403, 'Sorry! you cant see that.');
  469. * }
  470. * });
  471. * });
  472. *
  473. * @api public
  474. */
  475. sendFile(path: string): void;
  476. sendFile(path: string, options: any): void;
  477. sendFile(path: string, fn: Errback): void;
  478. sendFile(path: string, options: any, fn: Errback): void;
  479. /**
  480. * @deprecated Use sendFile instead.
  481. */
  482. sendfile(path: string): void;
  483. /**
  484. * @deprecated Use sendFile instead.
  485. */
  486. sendfile(path: string, options: any): void;
  487. /**
  488. * @deprecated Use sendFile instead.
  489. */
  490. sendfile(path: string, fn: Errback): void;
  491. /**
  492. * @deprecated Use sendFile instead.
  493. */
  494. sendfile(path: string, options: any, fn: Errback): void;
  495. /**
  496. * Transfer the file at the given `path` as an attachment.
  497. *
  498. * Optionally providing an alternate attachment `filename`,
  499. * and optional callback `fn(err)`. The callback is invoked
  500. * when the data transfer is complete, or when an error has
  501. * ocurred. Be sure to check `res.headerSent` if you plan to respond.
  502. *
  503. * This method uses `res.sendfile()`.
  504. */
  505. download(path: string): void;
  506. download(path: string, filename: string): void;
  507. download(path: string, fn: Errback): void;
  508. download(path: string, filename: string, fn: Errback): void;
  509. /**
  510. * Set _Content-Type_ response header with `type` through `mime.lookup()`
  511. * when it does not contain "/", or set the Content-Type to `type` otherwise.
  512. *
  513. * Examples:
  514. *
  515. * res.type('.html');
  516. * res.type('html');
  517. * res.type('json');
  518. * res.type('application/json');
  519. * res.type('png');
  520. *
  521. * @param type
  522. */
  523. contentType(type: string): Response;
  524. /**
  525. * Set _Content-Type_ response header with `type` through `mime.lookup()`
  526. * when it does not contain "/", or set the Content-Type to `type` otherwise.
  527. *
  528. * Examples:
  529. *
  530. * res.type('.html');
  531. * res.type('html');
  532. * res.type('json');
  533. * res.type('application/json');
  534. * res.type('png');
  535. *
  536. * @param type
  537. */
  538. type(type: string): Response;
  539. /**
  540. * Respond to the Acceptable formats using an `obj`
  541. * of mime-type callbacks.
  542. *
  543. * This method uses `req.accepted`, an array of
  544. * acceptable types ordered by their quality values.
  545. * When "Accept" is not present the _first_ callback
  546. * is invoked, otherwise the first match is used. When
  547. * no match is performed the server responds with
  548. * 406 "Not Acceptable".
  549. *
  550. * Content-Type is set for you, however if you choose
  551. * you may alter this within the callback using `res.type()`
  552. * or `res.set('Content-Type', ...)`.
  553. *
  554. * res.format({
  555. * 'text/plain': function(){
  556. * res.send('hey');
  557. * },
  558. *
  559. * 'text/html': function(){
  560. * res.send('<p>hey</p>');
  561. * },
  562. *
  563. * 'appliation/json': function(){
  564. * res.send({ message: 'hey' });
  565. * }
  566. * });
  567. *
  568. * In addition to canonicalized MIME types you may
  569. * also use extnames mapped to these types:
  570. *
  571. * res.format({
  572. * text: function(){
  573. * res.send('hey');
  574. * },
  575. *
  576. * html: function(){
  577. * res.send('<p>hey</p>');
  578. * },
  579. *
  580. * json: function(){
  581. * res.send({ message: 'hey' });
  582. * }
  583. * });
  584. *
  585. * By default Express passes an `Error`
  586. * with a `.status` of 406 to `next(err)`
  587. * if a match is not made. If you provide
  588. * a `.default` callback it will be invoked
  589. * instead.
  590. *
  591. * @param obj
  592. */
  593. format(obj: any): Response;
  594. /**
  595. * Set _Content-Disposition_ header to _attachment_ with optional `filename`.
  596. *
  597. * @param filename
  598. */
  599. attachment(filename?: string): Response;
  600. /**
  601. * Set header `field` to `val`, or pass
  602. * an object of header fields.
  603. *
  604. * Examples:
  605. *
  606. * res.set('Foo', ['bar', 'baz']);
  607. * res.set('Accept', 'application/json');
  608. * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
  609. *
  610. * Aliased as `res.header()`.
  611. */
  612. set(field: any): Response;
  613. set(field: string, value?: string): Response;
  614. header(field: any): Response;
  615. header(field: string, value?: string): Response;
  616. // Property indicating if HTTP headers has been sent for the response.
  617. headersSent: boolean;
  618. /**
  619. * Get value for header `field`.
  620. *
  621. * @param field
  622. */
  623. get (field: string): string;
  624. /**
  625. * Clear cookie `name`.
  626. *
  627. * @param name
  628. * @param options
  629. */
  630. clearCookie(name: string, options?: any): Response;
  631. /**
  632. * Set cookie `name` to `val`, with the given `options`.
  633. *
  634. * Options:
  635. *
  636. * - `maxAge` max-age in milliseconds, converted to `expires`
  637. * - `signed` sign the cookie
  638. * - `path` defaults to "/"
  639. *
  640. * Examples:
  641. *
  642. * // "Remember Me" for 15 minutes
  643. * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
  644. *
  645. * // save as above
  646. * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
  647. */
  648. cookie(name: string, val: string, options: CookieOptions): Response;
  649. cookie(name: string, val: any, options: CookieOptions): Response;
  650. cookie(name: string, val: any): Response;
  651. /**
  652. * Set the location header to `url`.
  653. *
  654. * The given `url` can also be the name of a mapped url, for
  655. * example by default express supports "back" which redirects
  656. * to the _Referrer_ or _Referer_ headers or "/".
  657. *
  658. * Examples:
  659. *
  660. * res.location('/foo/bar').;
  661. * res.location('http://example.com');
  662. * res.location('../login'); // /blog/post/1 -> /blog/login
  663. *
  664. * Mounting:
  665. *
  666. * When an application is mounted and `res.location()`
  667. * is given a path that does _not_ lead with "/" it becomes
  668. * relative to the mount-point. For example if the application
  669. * is mounted at "/blog", the following would become "/blog/login".
  670. *
  671. * res.location('login');
  672. *
  673. * While the leading slash would result in a location of "/login":
  674. *
  675. * res.location('/login');
  676. *
  677. * @param url
  678. */
  679. location(url: string): Response;
  680. /**
  681. * Redirect to the given `url` with optional response `status`
  682. * defaulting to 302.
  683. *
  684. * The resulting `url` is determined by `res.location()`, so
  685. * it will play nicely with mounted apps, relative paths,
  686. * `"back"` etc.
  687. *
  688. * Examples:
  689. *
  690. * res.redirect('/foo/bar');
  691. * res.redirect('http://example.com');
  692. * res.redirect(301, 'http://example.com');
  693. * res.redirect('http://example.com', 301);
  694. * res.redirect('../login'); // /blog/post/1 -> /blog/login
  695. */
  696. redirect(url: string): void;
  697. redirect(status: number, url: string): void;
  698. redirect(url: string, status: number): void;
  699. /**
  700. * Render `view` with the given `options` and optional callback `fn`.
  701. * When a callback function is given a response will _not_ be made
  702. * automatically, otherwise a response of _200_ and _text/html_ is given.
  703. *
  704. * Options:
  705. *
  706. * - `cache` boolean hinting to the engine it should cache
  707. * - `filename` filename of the view being rendered
  708. */
  709. render(view: string, options?: Object, callback?: (err: Error, html: string) => void ): void;
  710. render(view: string, callback?: (err: Error, html: string) => void ): void;
  711. locals: any;
  712. charset: string;
  713. }
  714. interface ErrorRequestHandler {
  715. (err: any, req: Request, res: Response, next: Function): any;
  716. }
  717. interface RequestHandler {
  718. (req: Request, res: Response, next: Function): any;
  719. }
  720. interface Handler extends RequestHandler {}
  721. interface RequestParamHandler {
  722. (req: Request, res: Response, next: Function, param: any): any;
  723. }
  724. interface Application extends IRouter<Application>, Express.Application {
  725. /**
  726. * Initialize the server.
  727. *
  728. * - setup default configuration
  729. * - setup default middleware
  730. * - setup route reflection methods
  731. */
  732. init(): void;
  733. /**
  734. * Initialize application configuration.
  735. */
  736. defaultConfiguration(): void;
  737. /**
  738. * Register the given template engine callback `fn`
  739. * as `ext`.
  740. *
  741. * By default will `require()` the engine based on the
  742. * file extension. For example if you try to render
  743. * a "foo.jade" file Express will invoke the following internally:
  744. *
  745. * app.engine('jade', require('jade').__express);
  746. *
  747. * For engines that do not provide `.__express` out of the box,
  748. * or if you wish to "map" a different extension to the template engine
  749. * you may use this method. For example mapping the EJS template engine to
  750. * ".html" files:
  751. *
  752. * app.engine('html', require('ejs').renderFile);
  753. *
  754. * In this case EJS provides a `.renderFile()` method with
  755. * the same signature that Express expects: `(path, options, callback)`,
  756. * though note that it aliases this method as `ejs.__express` internally
  757. * so if you're using ".ejs" extensions you dont need to do anything.
  758. *
  759. * Some template engines do not follow this convention, the
  760. * [Consolidate.js](https://github.com/visionmedia/consolidate.js)
  761. * library was created to map all of node's popular template
  762. * engines to follow this convention, thus allowing them to
  763. * work seamlessly within Express.
  764. */
  765. engine(ext: string, fn: Function): Application;
  766. /**
  767. * Assign `setting` to `val`, or return `setting`'s value.
  768. *
  769. * app.set('foo', 'bar');
  770. * app.get('foo');
  771. * // => "bar"
  772. * app.set('foo', ['bar', 'baz']);
  773. * app.get('foo');
  774. * // => ["bar", "baz"]
  775. *
  776. * Mounted servers inherit their parent server's settings.
  777. *
  778. * @param setting
  779. * @param val
  780. */
  781. set(setting: string, val: any): Application;
  782. get: {
  783. (name: string): any; // Getter
  784. (name: string, ...handlers: RequestHandler[]): Application;
  785. (name: RegExp, ...handlers: RequestHandler[]): Application;
  786. };
  787. /**
  788. * Return the app's absolute pathname
  789. * based on the parent(s) that have
  790. * mounted it.
  791. *
  792. * For example if the application was
  793. * mounted as "/admin", which itself
  794. * was mounted as "/blog" then the
  795. * return value would be "/blog/admin".
  796. */
  797. path(): string;
  798. /**
  799. * Check if `setting` is enabled (truthy).
  800. *
  801. * app.enabled('foo')
  802. * // => false
  803. *
  804. * app.enable('foo')
  805. * app.enabled('foo')
  806. * // => true
  807. */
  808. enabled(setting: string): boolean;
  809. /**
  810. * Check if `setting` is disabled.
  811. *
  812. * app.disabled('foo')
  813. * // => true
  814. *
  815. * app.enable('foo')
  816. * app.disabled('foo')
  817. * // => false
  818. *
  819. * @param setting
  820. */
  821. disabled(setting: string): boolean;
  822. /**
  823. * Enable `setting`.
  824. *
  825. * @param setting
  826. */
  827. enable(setting: string): Application;
  828. /**
  829. * Disable `setting`.
  830. *
  831. * @param setting
  832. */
  833. disable(setting: string): Application;
  834. /**
  835. * Configure callback for zero or more envs,
  836. * when no `env` is specified that callback will
  837. * be invoked for all environments. Any combination
  838. * can be used multiple times, in any order desired.
  839. *
  840. * Examples:
  841. *
  842. * app.configure(function(){
  843. * // executed for all envs
  844. * });
  845. *
  846. * app.configure('stage', function(){
  847. * // executed staging env
  848. * });
  849. *
  850. * app.configure('stage', 'production', function(){
  851. * // executed for stage and production
  852. * });
  853. *
  854. * Note:
  855. *
  856. * These callbacks are invoked immediately, and
  857. * are effectively sugar for the following:
  858. *
  859. * var env = process.env.NODE_ENV || 'development';
  860. *
  861. * switch (env) {
  862. * case 'development':
  863. * ...
  864. * break;
  865. * case 'stage':
  866. * ...
  867. * break;
  868. * case 'production':
  869. * ...
  870. * break;
  871. * }
  872. *
  873. * @param env
  874. * @param fn
  875. */
  876. configure(fn: Function): Application;
  877. configure(env0: string, fn: Function): Application;
  878. configure(env0: string, env1: string, fn: Function): Application;
  879. configure(env0: string, env1: string, env2: string, fn: Function): Application;
  880. configure(env0: string, env1: string, env2: string, env3: string, fn: Function): Application;
  881. configure(env0: string, env1: string, env2: string, env3: string, env4: string, fn: Function): Application;
  882. /**
  883. * Render the given view `name` name with `options`
  884. * and a callback accepting an error and the
  885. * rendered template string.
  886. *
  887. * Example:
  888. *
  889. * app.render('email', { name: 'Tobi' }, function(err, html){
  890. * // ...
  891. * })
  892. *
  893. * @param name
  894. * @param options or fn
  895. * @param fn
  896. */
  897. render(name: string, options?: Object, callback?: (err: Error, html: string) => void): void;
  898. render(name: string, callback: (err: Error, html: string) => void): void;
  899. /**
  900. * Listen for connections.
  901. *
  902. * A node `http.Server` is returned, with this
  903. * application (which is a `Function`) as its
  904. * callback. If you wish to create both an HTTP
  905. * and HTTPS server you may do so with the "http"
  906. * and "https" modules as shown here:
  907. *
  908. * var http = require('http')
  909. * , https = require('https')
  910. * , express = require('express')
  911. * , app = express();
  912. *
  913. * http.createServer(app).listen(80);
  914. * https.createServer({ ... }, app).listen(443);
  915. */
  916. listen(port: number, hostname: string, backlog: number, callback?: Function): http.Server;
  917. listen(port: number, hostname: string, callback?: Function): http.Server;
  918. listen(port: number, callback?: Function): http.Server;
  919. listen(path: string, callback?: Function): http.Server;
  920. listen(handle: any, listeningListener?: Function): http.Server;
  921. route(path: string): IRoute;
  922. router: string;
  923. settings: any;
  924. resource: any;
  925. map: any;
  926. locals: any;
  927. /**
  928. * The app.routes object houses all of the routes defined mapped by the
  929. * associated HTTP verb. This object may be used for introspection
  930. * capabilities, for example Express uses this internally not only for
  931. * routing but to provide default OPTIONS behaviour unless app.options()
  932. * is used. Your application or framework may also remove routes by
  933. * simply by removing them from this object.
  934. */
  935. routes: any;
  936. }
  937. interface Express extends Application {
  938. /**
  939. * Framework version.
  940. */
  941. version: string;
  942. /**
  943. * Expose mime.
  944. */
  945. mime: string;
  946. (): Application;
  947. /**
  948. * Create an express application.
  949. */
  950. createApplication(): Application;
  951. createServer(): Application;
  952. application: any;
  953. request: Request;
  954. response: Response;
  955. }
  956. var static: typeof serveStatic;
  957. }
  958. export = e;
  959. }