Created
June 16, 2025 03:39
-
-
Save opentechnologist/4b579bbda6907bc6f9f83ad66235f8a2 to your computer and use it in GitHub Desktop.
a simple example of a web server in express js that supports both http and https. es6 and es5 javascript compatible source codes are provided.
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
"use strict"; | |
var fs = require('fs'); | |
var http = require('http'); | |
var https = require('https'); | |
var express = require('express'); | |
var app = express(); | |
app.get('/', function (req, res) { | |
void req; | |
res.send("Date: ".concat(new Date())); | |
}); | |
var credentials; | |
try { | |
credentials = { | |
cert: fs.readFileSync('server.crt', 'utf8'), | |
key: fs.readFileSync('server.key', 'utf8'), | |
}; | |
} | |
catch (err) { | |
console.error('Failed reading SSL certificates:', err.message); | |
process.exit(1); | |
} | |
var httpServer = http.createServer(app); | |
var httpsServer = https.createServer(credentials, app); | |
var address = 'www1.localhost.test'; | |
var httpPort = 80; | |
var httpsPort = 443; | |
httpServer.listen(httpPort, address, function () { | |
console.log("HTTP server on http://".concat(address, ":").concat(httpPort, "/")); | |
}); | |
httpsServer.listen(httpsPort, address, function () { | |
console.log("HTTPS server on https://".concat(address, ":").concat(httpsPort, "/")); | |
}); | |
process.on('uncaughtException', function (err) { | |
console.error('Uncaught exception:', err); | |
}); | |
process.on('unhandledRejection', function (reason, promise) { | |
console.error('Unhandled Rejection at:', promise, 'reason:', reason); | |
}); |
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
const fs = require('fs'); | |
const http = require('http'); | |
const https = require('https'); | |
const express = require('express'); | |
const app = express(); | |
app.get('/', (req, res) => { | |
void req; | |
res.send(`The date today is: ${new Date()}`); | |
}); | |
let credentials; | |
try { | |
credentials = { | |
cert: fs.readFileSync('server.crt', 'utf8'), | |
key: fs.readFileSync('server.key', 'utf8'), | |
}; | |
} catch (err) { | |
console.error(`Failed reading SSL certificates: ${err.message}`); | |
process.exit(1); | |
} | |
const httpServer = http.createServer(app); | |
const httpsServer = https.createServer(credentials, app); | |
const address = 'www1.localhost.test'; | |
const httpPort = 80; | |
const httpsPort = 443; | |
httpServer.listen(httpPort, address, () => { | |
console.log(`HTTP server on http://${address}:${httpPort}/`); | |
}); | |
httpsServer.listen(httpsPort, address, () => { | |
console.log(`HTTPS server on https://${address}:${httpsPort}/`); | |
}); | |
process.on('uncaughtException', (err) => { | |
console.error('Uncaught exception:', err); | |
}); | |
process.on('unhandledRejection', (reason, promise) => { | |
console.error('Unhandled Rejection at:', promise, 'reason:', reason); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment