Created
August 3, 2018 06:08
-
-
Save scope2229/9cead1fb8c0c97da2892f7cff6d0221b to your computer and use it in GitHub Desktop.
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
const PORT = process.env.PORT || 3000 | |
const express = require('express'); | |
const app = express(); | |
const path = require('path') | |
const expressStaticGzip = require("express-static-gzip"); | |
const DIST_DIR = path.join(__dirname, "build") | |
app.use('/', expressStaticGzip(DIST_DIR, { | |
customCompressions: [{ | |
encodingName: 'deflate', | |
fileExtension: 'gz' | |
}] | |
})); | |
app.get('/', function(req, res, next) { | |
req.url = req.url + '.gz'; | |
res.set('Content-Encoding', 'gzip'); | |
res.set('Content-Type', 'html'); | |
res.sendFile(path.join(DIST_DIR, "index.html")); | |
}); | |
app.get('*.js', function(req, res, next) { | |
req.url = req.url + '.gz'; | |
res.set('Content-Encoding', 'gzip'); | |
res.set('Content-Type', 'text/javascript'); | |
next(); | |
}); | |
app.get('*.css', function(req, res, next) { | |
req.url = req.url + '.gz'; | |
res.set('Content-Encoding', 'gzip'); | |
res.set('Content-Type', 'text/css'); | |
next(); | |
}); | |
app.listen(PORT, function () { | |
console.log('Example app listening on port 3000!') | |
}) | |
//Serving the files on the dist folder | |
// //Serving the files on the dist folder | |
// // if (process.env.NODE_ENV === 'production') { | |
// // | |
// // app.get('/*', (req, res) => { | |
// // | |
// // }); | |
// // app.get('/*.js', (req, res) => { | |
// // | |
// // }); | |
// // } else { | |
// // app.use('/', router); | |
// // } | |
// | |
// | |
// //Send index.html when the user access the web | |
// app.get("/", function (req, res) { | |
// res.sendFile(path.join(DIST_DIR, "index.html")); | |
// }); | |
// | |
// app.listen(PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment