UNPKG

3.24 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import * as http from "http";
4
5/**
6 * Create a new connect server.
7 */
8declare function createServer(): createServer.Server;
9
10declare namespace createServer {
11 export type ServerHandle = HandleFunction | http.Server;
12
13 export class IncomingMessage extends http.IncomingMessage {
14 originalUrl?: http.IncomingMessage["url"] | undefined;
15 }
16
17 type NextFunction = (err?: any) => void;
18
19 export type SimpleHandleFunction = (req: IncomingMessage, res: http.ServerResponse) => void;
20 export type NextHandleFunction = (req: IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;
21 export type ErrorHandleFunction = (
22 err: any,
23 req: IncomingMessage,
24 res: http.ServerResponse,
25 next: NextFunction,
26 ) => void;
27 export type HandleFunction = SimpleHandleFunction | NextHandleFunction | ErrorHandleFunction;
28
29 export interface ServerStackItem {
30 route: string;
31 handle: ServerHandle;
32 }
33
34 export interface Server extends NodeJS.EventEmitter {
35 (req: http.IncomingMessage, res: http.ServerResponse, next?: Function): void;
36
37 route: string;
38 stack: ServerStackItem[];
39
40 /**
41 * Utilize the given middleware `handle` to the given `route`,
42 * defaulting to _/_. This "route" is the mount-point for the
43 * middleware, when given a value other than _/_ the middleware
44 * is only effective when that segment is present in the request's
45 * pathname.
46 *
47 * For example if we were to mount a function at _/admin_, it would
48 * be invoked on _/admin_, and _/admin/settings_, however it would
49 * not be invoked for _/_, or _/posts_.
50 */
51 use(fn: NextHandleFunction): Server;
52 use(fn: HandleFunction): Server;
53 use(route: string, fn: NextHandleFunction): Server;
54 use(route: string, fn: HandleFunction): Server;
55
56 /**
57 * Handle server requests, punting them down
58 * the middleware stack.
59 */
60 handle(req: http.IncomingMessage, res: http.ServerResponse, next: Function): void;
61
62 /**
63 * Listen for connections.
64 *
65 * This method takes the same arguments
66 * as node's `http.Server#listen()`.
67 *
68 * HTTP and HTTPS:
69 *
70 * If you run your application both as HTTP
71 * and HTTPS you may wrap them individually,
72 * since your Connect "server" is really just
73 * a JavaScript `Function`.
74 *
75 * var connect = require('connect')
76 * , http = require('http')
77 * , https = require('https');
78 *
79 * var app = connect();
80 *
81 * http.createServer(app).listen(80);
82 * https.createServer(options, app).listen(443);
83 */
84 listen(port: number, hostname?: string, backlog?: number, callback?: Function): http.Server;
85 listen(port: number, hostname?: string, callback?: Function): http.Server;
86 listen(path: string, callback?: Function): http.Server;
87 listen(handle: any, listeningListener?: Function): http.Server;
88 }
89}
90
91export = createServer;