|
1 # SPDY Server for node.js [](http://travis-ci.org/indutny/node-spdy) |
|
2 |
|
3 <a href="http://flattr.com/thing/758213/indutnynode-spdy-on-GitHub" target="_blank"> |
|
4 <img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a> |
|
5 |
|
6 With this module you can create [SPDY](http://www.chromium.org/spdy) servers |
|
7 in node.js with natural http module interface and fallback to regular https |
|
8 (for browsers that doesn't support SPDY yet). |
|
9 |
|
10 ## Usage |
|
11 |
|
12 ```javascript |
|
13 var spdy = require('spdy'), |
|
14 fs = require('fs'); |
|
15 |
|
16 var options = { |
|
17 key: fs.readFileSync(__dirname + '/keys/spdy-key.pem'), |
|
18 cert: fs.readFileSync(__dirname + '/keys/spdy-cert.pem'), |
|
19 ca: fs.readFileSync(__dirname + '/keys/spdy-csr.pem'), |
|
20 |
|
21 // SPDY-specific options |
|
22 windowSize: 1024, // Server's window size |
|
23 }; |
|
24 |
|
25 var server = spdy.createServer(options, function(req, res) { |
|
26 res.writeHead(200); |
|
27 res.end('hello world!'); |
|
28 }); |
|
29 |
|
30 server.listen(443); |
|
31 ``` |
|
32 |
|
33 And by popular demand - usage with |
|
34 [express](https://github.com/visionmedia/express): |
|
35 |
|
36 ```javascript |
|
37 var spdy = require('spdy'), |
|
38 express = require('express'), |
|
39 fs = require('fs'); |
|
40 |
|
41 var options = { /* the same as above */ }; |
|
42 |
|
43 var app = express(); |
|
44 |
|
45 app.use(/* your favorite middleware */); |
|
46 |
|
47 var server = spdy.createServer(options, app); |
|
48 |
|
49 server.listen(443); |
|
50 ``` |
|
51 |
|
52 ## API |
|
53 |
|
54 API is compatible with `http` and `https` module, but you can use another |
|
55 function as base class for SPDYServer. |
|
56 |
|
57 ```javascript |
|
58 spdy.createServer( |
|
59 [base class constructor, i.e. https.Server], |
|
60 { /* keys and options */ }, // <- the only one required argument |
|
61 [request listener] |
|
62 ).listen([port], [host], [callback]); |
|
63 ``` |
|
64 |
|
65 Request listener will receive two arguments: `request` and `response`. They're |
|
66 both instances of `http`'s `IncomingMessage` and `OutgoingMessage`. But three |
|
67 custom properties are added to both of them: `streamID`, `isSpdy`, |
|
68 `spdyVersion`. The first one indicates on which spdy stream are sitting request |
|
69 and response. Second is always true and can be checked to ensure that incoming |
|
70 request wasn't received by HTTPS fallback and last one is a number representing |
|
71 used SPDY protocol version (2 or 3 for now). |
|
72 |
|
73 ### Push streams |
|
74 |
|
75 It is possible to initiate 'push' streams to send content to clients _before_ |
|
76 the client requests it. |
|
77 |
|
78 ```javascript |
|
79 spdy.createServer(options, function(req, res) { |
|
80 var headers = { 'content-type': 'application/javascript' }; |
|
81 res.push('/main.js', headers, function(err, stream) { |
|
82 if (err) return; |
|
83 |
|
84 stream.end('alert("hello from push stream!");'); |
|
85 }); |
|
86 |
|
87 res.end('<script src="/main.js"></script>'); |
|
88 }).listen(443); |
|
89 ``` |
|
90 |
|
91 Push is accomplished via the `push()` method invoked on the current response |
|
92 object (this works for express.js response objects as well). The format of the |
|
93 `push()` method is: |
|
94 |
|
95 `.push('full or relative url', { ... headers ... }, optional priority, callback)` |
|
96 |
|
97 You can use either full ( `http://host/path` ) or relative ( `/path` ) urls with |
|
98 `.push()`. `headers` are the same as for regular response object. `callback` |
|
99 will receive two arguments: `err` (if any error is happened) and `stream` |
|
100 (stream object have API compatible with a |
|
101 [net.Socket](http://nodejs.org/docs/latest/api/net.html#net.Socket) ). |
|
102 |
|
103 ### Options |
|
104 |
|
105 All options supported by |
|
106 [tls](http://nodejs.org/docs/latest/api/tls.html#tls.createServer) are working |
|
107 with node-spdy. In addition, `maxStreams` options is available. it allows you |
|
108 controlling [maximum concurrent streams][http://www.chromium.org/spdy/spdy-protocol/spdy-protocol-draft2#TOC-SETTINGS] |
|
109 protocol option (if client will start more streams than that limit, RST_STREAM |
|
110 will be sent for each additional stream). |
|
111 |
|
112 Additional options: |
|
113 |
|
114 * `plain` - if defined, server will accept only plain (non-encrypted) |
|
115 connections. |
|
116 |
|
117 #### Contributors |
|
118 |
|
119 * [Fedor Indutny](https://github.com/indutny) |
|
120 * [Chris Strom](https://github.com/eee-c) |
|
121 * [François de Metz](https://github.com/francois2metz) |
|
122 * [Ilya Grigorik](https://github.com/igrigorik) |
|
123 * [Roberto Peon](https://github.com/grmocg) |
|
124 * [Tatsuhiro Tsujikawa](https://github.com/tatsuhiro-t) |
|
125 * [Jesse Cravens](https://github.com/jessecravens) |
|
126 |
|
127 #### LICENSE |
|
128 |
|
129 This software is licensed under the MIT License. |
|
130 |
|
131 Copyright Fedor Indutny, 2012. |
|
132 |
|
133 Permission is hereby granted, free of charge, to any person obtaining a |
|
134 copy of this software and associated documentation files (the |
|
135 "Software"), to deal in the Software without restriction, including |
|
136 without limitation the rights to use, copy, modify, merge, publish, |
|
137 distribute, sublicense, and/or sell copies of the Software, and to permit |
|
138 persons to whom the Software is furnished to do so, subject to the |
|
139 following conditions: |
|
140 |
|
141 The above copyright notice and this permission notice shall be included |
|
142 in all copies or substantial portions of the Software. |
|
143 |
|
144 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
145 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
146 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
|
147 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
|
148 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
|
149 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
|
150 USE OR OTHER DEALINGS IN THE SOFTWARE. |