Skip to content

Instantly share code, notes, and snippets.

@dkarmalita
Last active February 21, 2017 16:22
Show Gist options
  • Save dkarmalita/4842cd0044cacd1bd4184bc27f150bc8 to your computer and use it in GitHub Desktop.
Save dkarmalita/4842cd0044cacd1bd4184bc27f150bc8 to your computer and use it in GitHub Desktop.
Simple express server with compression enabled.
/**
* Simple express server with a dynamic gz compression enabled.
*/
"use strict";
const express = require("express");
const cors = require("cors");
const compression = require('compression');
const morgan = require('morgan');
const logger = function (req, res, next) {
// custom logger which can be activated with .use(logger)
console.log(Date.now(), req.headers['accept-encoding']);
console.log(Date.now(), req.body);
next();
};
express()
.use(cors()) // allow all of the cors
.use(compression()) // allow gzip cmpression (express >= 3)
// When enabled, compression middleware gzips all of the requested files
// cons: lower traffic, pros: cpu utilization + responce time
.use(morgan(':method :url :status :res[content-length] - :response-time ms - :req[accept-encoding]'))
.use(express.static('public')) // source of the files
.listen(3001); // port to serve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment