Skip to content

Commit 332c0f8

Browse files
committed
express 4.x middleware
- response-time - serve-favicon - serve-static
1 parent 5e8ac23 commit 332c0f8

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

response-time/response-time.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Type definitions for response-time 2.2.0
2+
// Project: https://github.com/expressjs/response-time
3+
// Definitions by: Uros Smolnik <https://github.com/urossmolnik/>
4+
// DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped
5+
6+
/* =================== USAGE ===================
7+
8+
import responseTime = require('response-time');
9+
app.use(responseTime());
10+
11+
=============================================== */
12+
13+
/// <reference path="../express/express.d.ts" />
14+
15+
declare module "response-time" {
16+
import express = require('express');
17+
18+
/**
19+
* Response time header for node.js
20+
* Returns middleware that adds a X-Response-Time header to responses.
21+
*/
22+
function responseTime(options?: {
23+
/**
24+
* The fixed number of digits to include in the output, which is always in milliseconds, defaults to 3 (ex: 2.300ms).
25+
*/
26+
digits?: number;
27+
/**
28+
* The name of the header to set, defaults to X-Response-Time.
29+
*/
30+
header?: string;
31+
/**
32+
* Boolean to indicate if units of measurement suffix should be added to the output, defaults to true (ex: 2.300ms vs 2.300).
33+
*/
34+
suffix?: boolean;
35+
}): express.RequestHandler;
36+
37+
export = responseTime;
38+
}

serve-favicon/serve-favicon.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Type definitions for serve-favicon 2.1.6
2+
// Project: https://github.com/expressjs/serve-favicon
3+
// Definitions by: Uros Smolnik <https://github.com/urossmolnik/>
4+
// DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped
5+
6+
/* =================== USAGE ===================
7+
8+
import serveFavicon = require('serve-favicon');
9+
app.use(serveFavicon(__dirname + '/public/favicon.ico'));
10+
11+
=============================================== */
12+
13+
/// <reference path="../express/express.d.ts" />
14+
15+
declare module "serve-favicon" {
16+
import express = require('express');
17+
18+
/**
19+
* Node.js middleware for serving a favicon.
20+
*/
21+
function serveFavicon(path: string, options?: {
22+
/**
23+
* The cache-control max-age directive in ms, defaulting to 1 day. This can also be a string accepted by the ms module.
24+
*/
25+
maxAge?: number;
26+
}): express.RequestHandler;
27+
28+
export = serveFavicon;
29+
}

serve-static/serve-static.d.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Type definitions for serve-static 1.7.1
2+
// Project: https://github.com/expressjs/serve-static
3+
// Definitions by: Uros Smolnik <https://github.com/urossmolnik/>
4+
// DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped
5+
6+
/* =================== USAGE ===================
7+
8+
import serveStatic = require('serve-static');
9+
app.use(serveStatic('public/ftp', {'index': ['default.html', 'default.htm']}))
10+
11+
=============================================== */
12+
13+
/// <reference path="../express/express.d.ts" />
14+
15+
declare module "serve-static" {
16+
import express = require('express');
17+
18+
/**
19+
* Create a new middleware function to serve files from within a given root directory.
20+
* The file to serve will be determined by combining req.url with the provided root directory.
21+
* When a file is not found, instead of sending a 404 response, this module will instead call next() to move on to the next middleware, allowing for stacking and fall-backs.
22+
*/
23+
function serveStatic(root: string, options?: {
24+
/**
25+
* Set how "dotfiles" are treated when encountered. A dotfile is a file or directory that begins with a dot (".").
26+
* Note this check is done on the path itself without checking if the path actually exists on the disk.
27+
* If root is specified, only the dotfiles above the root are checked (i.e. the root itself can be within a dotfile when when set to "deny").
28+
* The default value is 'ignore'.
29+
* 'allow' No special treatment for dotfiles
30+
* 'deny' Send a 403 for any request for a dotfile
31+
* 'ignore' Pretend like the dotfile does not exist and call next()
32+
*/
33+
dotfiles?: string;
34+
35+
/**
36+
* Enable or disable etag generation, defaults to true.
37+
*/
38+
etag?: boolean;
39+
40+
/**
41+
* Set file extension fallbacks. When set, if a file is not found, the given extensions will be added to the file name and search for.
42+
* The first that exists will be served. Example: ['html', 'htm'].
43+
* The default value is false.
44+
*/
45+
extensions?: boolean;
46+
47+
/**
48+
* By default this module will send "index.html" files in response to a request on a directory.
49+
* To disable this set false or to supply a new index pass a string or an array in preferred order.
50+
*/
51+
index?: boolean;
52+
53+
/**
54+
* Enable or disable Last-Modified header, defaults to true. Uses the file system's last modified value.
55+
*/
56+
lastModified?: boolean;
57+
58+
/**
59+
* Provide a max-age in milliseconds for http caching, defaults to 0. This can also be a string accepted by the ms module.
60+
*/
61+
maxAge?: number;
62+
63+
/**
64+
* Redirect to trailing "/" when the pathname is a dir. Defaults to true.
65+
*/
66+
redirect?: number;
67+
68+
/**
69+
* Function to set custom headers on response. Alterations to the headers need to occur synchronously.
70+
* The function is called as fn(res, path, stat), where the arguments are:
71+
* res the response object
72+
* path the file path that is being sent
73+
* stat the stat object of the file that is being sent
74+
*/
75+
setHeaders?: (res, path, stat) => any;
76+
}): express.Handler;
77+
78+
export = serveStatic;
79+
}

0 commit comments

Comments
 (0)