Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ezekielchentnik
Created September 17, 2018 13:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezekielchentnik/c80a828bfa92f282f1570ba5b0b6d228 to your computer and use it in GitHub Desktop.
Save ezekielchentnik/c80a828bfa92f282f1570ba5b0b6d228 to your computer and use it in GitHub Desktop.
const fastify = require('fastify');
const fastifyAutoPush = require('fastify-auto-push');
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const fsReadFile = promisify(fs.readFile);
const STATIC_DIR = path.join(__dirname, 'static');
const CERTS_DIR = path.join(__dirname, 'certs');
const PORT = 8080;
async function createServerOptions() {
const readCertFile = (filename) => {
return fsReadFile(path.join(CERTS_DIR, filename));
};
const [key, cert] = await Promise.all(
[readCertFile('server.key'), readCertFile('server.crt')]);
return {key, cert};
}
async function main() {
const {key, cert} = await createServerOptions();
// Browsers support only https for HTTP/2.
const app = fastify({https: {key, cert}, http2: true});
// Create and register AutoPush plugin. It should be registered as the first
// in the middleware chain.
app.register(fastifyAutoPush.staticServe, {root: STATIC_DIR});
await app.listen(PORT);
console.log(`Listening on port ${PORT}`);
}
main().catch((err) => {
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment