SPDY Server for node.js
Last updated
Was this helpful?
Last updated
Was this helpful?
With this module you can create HTTP2 / SPDY servers in node.js with natural http module interface and fallback to regular https (for browsers that don't support neither HTTP2, nor SPDY yet).
This module named spdy
but it provides support for both http/2 (h2) and spdy (2,3,3.1). Also, spdy
is compatible with Express.
Server:
Client:
Please note that if you use a custom agent, by default all connection-level errors will result in an uncaught exception. To handle these errors subscribe to the error
event and re-emit the captured error:
It is possible to initiate PUSH_PROMISE to send content to clients before the client requests it.
PUSH_PROMISE may be sent using the push()
method on the current response object. The signature of the push()
method is:
.push('/some/relative/url', { request: {...}, response: {...} }, callback)
Second argument contains headers for both PUSH_PROMISE and emulated response. callback
will receive two arguments: err
(if any error is happened) and a Duplex stream as the second argument.
Client usage:
NOTE: You're responsible for the stream
object once given it in .push()
callback or push
event. Hence ignoring error
event on it will result in uncaught exception and crash your program.
Server usage:
Client usage:
All options supported by tls work with node-spdy.
Additional options may be passed via spdy
sub-object:
plain
- if defined, server will ignore NPN and ALPN data and choose whether to use spdy or plain http by looking at first data packet.
ssl
- if false
and options.plain
is true
, http.Server
will be used as a base
class for created server.
maxChunk
- if set and non-falsy, limits number of bytes sent in one DATA chunk. Setting it to non-zero value is recommended if you care about interleaving of outgoing data from multiple different streams. (defaults to 8192)
protocols
- list of NPN/ALPN protocols to use (default is: ['h2','spdy/3.1', 'spdy/3', 'spdy/2','http/1.1', 'http/1.0']
)
protocol
- use specific protocol if no NPN/ALPN ex In addition,
maxStreams
- set "maximum concurrent streams" protocol option
API is compatible with http
and https
module, but you can use another function as base class for SPDYServer.
Request listener will receive two arguments: request
and response
. They're both instances of http
's IncomingMessage
and OutgoingMessage
. But three custom properties are added to both of them: isSpdy
, spdyVersion
. isSpdy
is true
when the request was processed using HTTP2/SPDY protocols, it is false
in case of HTTP/1.1 fallback. spdyVersion
is either of: 2
, 3
, 3.1
, or 4
(for HTTP2).
This software is licensed under the MIT License.
Copyright Fedor Indutny, 2015.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.