Skip to content

Instantly share code, notes, and snippets.

@idiomatic
Created June 7, 2018 16:45
Show Gist options
  • Save idiomatic/c0502d8e09ebee76092f891950bad038 to your computer and use it in GitHub Desktop.
Save idiomatic/c0502d8e09ebee76092f891950bad038 to your computer and use it in GitHub Desktop.

Legacy Node.js

const http = require('http');
const https = require('https');
const http2 = require('http2');
const s = http.createServer((req, res) => { });
s.listen(PORT, ADDRESS);
const options = {key: KEY, cert: CERT};
const stls = https.createServer(options, (req, res) => { });
stls.listen(PORT_TLS, ADDRESS);
const s2 = http2.createSecureServer(options, (req, res) => { });
s2.listen(PORT_HTTP2, ADDRESS);

Go

package main

import "net/http"

func main() {
    s := &http.Server{Addr:"ADDRESS:PORT"}
    s.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { })
    go func() {
        s.ListenAndServe()
    }()

    stls := &http.Server{Addr:"ADDRESS:PORT_TLS"}
    stls.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { })
    go func() {
        stls.ListenAndServeTLS()
    }()

    // wait forever
    select{ }
}

NODEISH

Analagous to readFileSync() and Node.js.

import { createServer, createTLSServer, createSecureServer } from "deno";
const options = {key: KEY, cert: CERT};
const s = createServer((req, res) => { });
const stls = createTLSServer(options, (req, res) => { });
const s2 = createSecureServer(options, (req, res) => { });
s.listen(PORT, ADDRESS);
stls.listen(PORT_TLS, ADDRESS);
s2.listen(PORT_HTTP2, ADDRESS);

MANYLISTENS

Distinct listen() methods.

import { createServer } from "deno";
const options = {key: KEY, cert: CERT};
const s = createServer(options, (req, res) => { });
s.listen(PORT, ADDRESS);
s.listenTLS(PORT_TLS, ADDRESS);
s.listen2(PORT_HTTP2, ADDRESS);

MANYCREATES

Explicit constructors and flat namespace.

import { createHTTPServer, createTLSServer, createHTTP2Server } from "deno";
const options = {key: KEY, cert: CERT};
const s = createHTTPServer((req, res) => { });
const stls = createTLSServer(options, (req, res) => { });
const s2 = createHTTP2Server(options, (req, res) => { });
s.listen(PORT, ADDRESS);
stls.listen(PORT_TLS, ADDRESS);
s2.listen(PORT_HTTP2, ADDRESS);

NAMESPACES

Node.js-like namespaces to disambiguate createServer().

import { http, https, http2 } from "deno";
const options = {key: KEY, cert: CERT};
const s = http.createServer((req, res) => { });
const stls = https.createServer(options, (req, res) => { });
const s2 = http2.createServer(options, (req, res) => { });
s.listen(PORT, ADDRESS);
stls.listen(PORT_TLS, ADDRESS);
s2.listen(PORT_HTTP2, ADDRESS);

CREATEOPT

createServer() attribute.

import { createServer } from "deno";
const options = {key: KEY, cert: CERT};
const s = createServer((req, res) => { });
const stls = createServer({...options, protocol: "https"}, (req, res) => { });
const s2 = createServer({...options, protocol: "http/2"}, (req, res) => { });
s.listen(PORT, ADDRESS)
stls.listen(PORT_TLS, ADDRESS)
s2.listen(PORT_HTTP2, ADDRESS)

LISTENARG

listen() method argument.

import { createServer } from "deno";
const options = {key: KEY, cert: CERT};
const s = createServer(options, (req, res) => { });
s.listen(PORT, ADDRESS, "http");
s.listen({protocol: "https"}, PORT_TLS, ADDRESS);
s.listen({protocol: "http/2"}, PORT_HTTP2, ADDRESS);

LISTENMAP

listen() port map

import { createServer } from "deno";
const options = {key: KEY, cert: CERT};
const s = createServer(options, (req, res) => { });
const portMap = {80:"http", 443:["https", "http/2"]};
s.listen(portMap, ADDRESS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment