Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Last active Jun 28, 2020
Embed
What would you like to do?
Minimal Node Servers

Minimal Node Servers

A collection of code snippets that create servers in as few steps as possible.

Minimal HTTP Server

// setup (61 bytes):
const http = require('http');

const server = new http.Server;

// usage:
server.on('listening', function () {
  console.log('listening: http://localhost/');
}).on('request', function (request, response) {
  console.log('request: http://localhost' + request.url);
  response.end(request.url);
}).listen(80);

Minimal HTTPS Server

// setup (66 bytes):
const https = require('https');

const server = new https.Server({ /* cert, key */ });

// usage:
server.on('listening', function () {
  console.log('listening: https://localhost/');
}).on('request', function (request, response) {
  console.log('request: https://localhost' + request.url);
  response.end(request.url);
}).listen(443);

Minimal HTTP/HTTPS Server

// setup (200 bytes):
const http = require('http');
const https = require('https');

const server = new https.Server({ /* cert, key */ });

const onHttp = http._connectionListener;
const onHttps = server._events.connection;
const onAny = server._events.connection = function (socket) {
  const data = socket.read(1);

  if (data) {
    socket.unshift(data);

    (data[0] === 22 ? onHttps : onHttp).call(this, socket);
  } else {
    socket.once('readable', onAny.bind(this, socket));
  }
};

// usage:
server.on('listening', function () {
  const site = '://localhost:' + this.address().port + '/';

  console.log('listening: http' + site + ' & https' + site);
}).on('request', function (request, response) {
  const protocol = request.connection.encrypted ? 'https' : 'http';
  const href = protocol + '://localhost:' + request.connection.localPort + request.url;

  console.log('request: ' + href);
  response.end(request.url);
}).listen(80); /* 80 or 443 */

Minimal HTTP/HTTPS Multi-Port Server

// setup (200 bytes):
const http = require('http');
const https = require('https');

const server = new https.Server({ /* cert, key */ });

const onHttp = http._connectionListener;
const onHttps = server._events.connection;
const onAny = server._events.connection = function (socket) {
  const data = socket.read(1);

  if (data) {
    socket.unshift(data);

    (data[0] === 22 ? onHttps : onHttp).call(this, socket);
  } else {
    socket.once('readable', onAny.bind(this, socket));
  }
};

// usage:
server.on('listening', function () {
  const site = '://localhost:' + this.address().port + '/';

  console.log('listening: http' + site + ' & https' + site);
}).on('request', function (request, response) {
  const protocol = request.connection.encrypted ? 'https' : 'http';
  const href = protocol + '://localhost:' + request.connection.localPort + request.url;

  console.log('request: ' + href);
  response.end(request.url);
});

Object.create(server).listen(80);
Object.create(server).listen(443);

Minimal HTTP/WebSocket Server

// setup (382 bytes):
const crypto = require('crypto');
const http = require('http');
const WebSocketConnection = require('websocket/lib/WebSocketConnection');

const server = new http.Server;

server.on('upgrade', function (request, socket) {
  if (request.headers.upgrade === 'websocket') {
    socket.write([
      'HTTP/1.1 101 Switching Protocols',
      'Upgrade: websocket',
      'Connection: Upgrade',
      'Sec-WebSocket-Accept: ' + crypto.createHash('sha1').update(request.headers['sec-websocket-key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', 'binary').digest('base64'),
      '', ''
    ].join('\r\n'));

    this.ws = new WebSocketConnection(socket, [], 'echo-protocol', false, { /* configuration, see https://github.com/theturtle32/WebSocket-Node/blob/master/docs/WebSocketServer.md#server-config-options */ });

    this.ws._addSocketEventListeners();
    
    this.emit('wsconnection', this.ws);
  }
});

// usage:
server.on('listening', function () {
  console.log('listening: http://localhost/');
}).on('request', function (request, response) {
  console.log('request', request.url);
  response.end(request.url);
}).on('wsconnection', function (ws) {
  ws.on('message', function (message) {
    if (message.type === 'utf8') {
      console.log('Received: "' + message.utf8Data + '"');

      ws.sendUTF(message.utf8Data.toUpperCase() + '!!!');
    }
  });
}).listen(80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment