Last active
March 31, 2017 13:16
-
-
Save Paul-frc/99277f7b77c92b5f1b04b5b5cf1a4c4f to your computer and use it in GitHub Desktop.
http and https server with gulp and express
This file contains hidden or 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
/* TODO | |
try fallback directory for ssl certs and keys, if no others found... | |
*/ | |
/* | |
npm install gulp file-system spdy express compression dotenv --save-dev | |
*/ | |
var gulp = require('gulp'); | |
var fs = require('file-system'); | |
var http = require('http'); | |
var http2 = require('spdy'); | |
var express = require('express'); | |
var compression = require('compression'); | |
var serveStatic = require('serve-static'); | |
var runSequence = require('run-sequence'); | |
var del = require('del'); | |
require('dotenv').config(); | |
var sslCrt, sslKey; | |
fs.open('./.env', 'r', (err) => { | |
if (err) { | |
if (err.code === 'ENOENT') { | |
console.log('.env file not found!!'); | |
return; | |
} | |
throw err; | |
} else { | |
fs.readFile('./.env', 'utf8', (err, data) => { | |
if(data.indexOf('SSL_CRT_PATH') < 0){ | |
console.log("no 'SSL_CRT_PATH' found!!"); | |
} else { | |
sslCrt = process.env.SSL_CRT_PATH.toString(); | |
sslKey = process.env.SSL_KEY_PATH.toString(); | |
} | |
}) | |
} | |
}); | |
// https server with gzip and http2 | |
gulp.task('https-server', function(){ | |
var privateKey = fs.readFileSync(sslKey, 'utf8'); | |
var certificate = fs.readFileSync(sslCrt, 'utf8'); | |
var credentials = {key: privateKey, cert: certificate}; | |
var app = express(); | |
app.use(compression()) | |
app.use(serveStatic('./dist', { | |
'extensions': ['html'], | |
'maxAge': 3600000 | |
})) | |
var httpsServer = http2.createServer(credentials, app); | |
httpsServer.listen(8889); | |
console.log("https://localhost:8889") | |
}) | |
// http server with gzip | |
gulp.task('http-server', function(){ | |
var app = express(); | |
app.use(compression()) | |
app.use(serveStatic('./dist', { | |
'extensions': ['html'], | |
'maxAge': 3600000 | |
})) | |
var httpServer = http.createServer(app); | |
httpServer.listen(8888); | |
console.log("http://localhost:8888") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment