What is spdy?
The spdy npm package is designed to support the SPDY and HTTP/2 protocols in Node.js. It provides server and client functionality, allowing developers to create SPDY/HTTP2 servers and clients with ease. This package is particularly useful for improving web application performance by leveraging the advanced features of these protocols, such as multiplexing, server push, and header compression.
What are spdy's main functionalities?
Creating an SPDY/HTTP2 server
This code sample demonstrates how to create a simple SPDY/HTTP2 server using the spdy package along with Express. It sets up a server that listens on port 3000 and serves a simple message over SPDY/HTTP2.
const spdy = require('spdy');
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.end('Hello over SPDY/HTTP2');
});
const options = {
key: fs.readFileSync('<path-to-key>'),
cert: fs.readFileSync('<path-to-cert>')
};
spdy.createServer(options, app).listen(3000, () => {
console.log('Server is running on https://localhost:3000');
});
Creating an SPDY/HTTP2 client
This code sample shows how to create an SPDY/HTTP2 client that connects to a server. It demonstrates making a request to the server and handling the response, including reading response headers and data.
const spdy = require('spdy');
const http2 = require('http2');
const client = spdy.connect('https://localhost:3000', (err, socket) => {
if (err) {
throw new Error('Connection failed');
}
const req = http2.request({
':path': '/'
});
req.on('response', (headers) => {
console.log('Response headers:', headers);
});
req.setEncoding('utf8');
req.on('data', (chunk) => console.log(chunk));
req.end();
});
Other packages similar to spdy
http2
The http2 package is a core module in Node.js that provides an implementation of the HTTP/2 protocol. It offers similar functionalities to spdy, such as creating servers and clients that can communicate over HTTP/2. However, spdy provides additional support for the SPDY protocol, which is not covered by the http2 module.
node-http2
node-http2 is an npm package that also implements the HTTP/2 protocol. It provides an API for creating HTTP/2 servers and clients. Compared to spdy, node-http2 focuses solely on HTTP/2 without support for SPDY. spdy might offer a more comprehensive solution for developers looking to support both protocols.
SPDY Server for node.js
With this module you can create SPDY servers
in node.js with natural http module interface and fallback to regular https
(for browsers that doesn't support SPDY yet).
It's using SSL's NPN feature that is available in node from 0.6.0 version, but
requires you to build node with latest openssl.
Node+OpenSSL building
At the moment node-spdy requires zlib dictionary support, which will come to
node.js only in 0.7.x version. To build 0.7.x version follow instructions below:
git clone git://github.com/joyent/node.git
cd node
./configure --prefix=$HOME/.node/dev
make install -j4
echo 'export PATH=$HOME/.node/dev/bin:$PATH' >> ~/.bashrc
Usage
var spdy = require('spdy');
var options = {
key: fs.readFileSync(__dirname + '/../keys/spdy-key.pem'),
cert: fs.readFileSync(__dirname + '/../keys/spdy-cert.pem'),
ca: fs.readFileSync(__dirname + '/../keys/spdy-csr.pem')
};
spdy.createServer(options, function(req, res) {
res.writeHead(200);
res.end('hello world!');
});
spdy.listen(443);
Helping project
Node-spdy is open for donations, please feel free to contact me for any futher information: fedor@indutny.com
Contributors
LICENSE
This software is licensed under the MIT License.
Copyright Fedor Indutny, 2012.
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.