Last active
February 21, 2017 16:22
-
-
Save dkarmalita/4842cd0044cacd1bd4184bc27f150bc8 to your computer and use it in GitHub Desktop.
Simple express server with compression enabled.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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