Created
April 19, 2018 11:51
-
-
Save jsuryahyd/d1dac0d947f9de52411e2be23cee3589 to your computer and use it in GitHub Desktop.
express server run with both http and https
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 express = require('express'); | |
const path = require('path'); | |
const util = require('util'); | |
const fs = require('fs'); | |
const morgan = require('morgan') | |
const https = require('https') | |
const app = express(); | |
//static | |
app.use(express.static(path.join(__dirname,'./public'))); | |
app.use(morgan('tiny')) | |
app.get('/',(req,res)=>{ | |
res.sendFile('main.html',{root:__dirname+'/views'}); | |
}); | |
const port = 50001; | |
app.listen(port,()=>{ | |
console.log('listening on localhost:'+port) | |
}); | |
app.use(function(err, req, res, next) { | |
// Do logging and user-friendly error message display | |
res.status(500).send({status:500, message: 'internal error', type:'internal'}); | |
console.error(err); | |
}) | |
// https | |
const certificate = { | |
key:fs.readFileSync(path.join(__dirname,'../build/myCert.test.key')), | |
cert:fs.readFileSync(path.join(__dirname,'../build/myCert.test.crt')) | |
} | |
https.createServer(certificate,app).listen(443) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment