Skip to content

Instantly share code, notes, and snippets.

@vpadhariya
Created January 6, 2024 04:30
Show Gist options
  • Save vpadhariya/8c322370db5e335b59dd980a519394c0 to your computer and use it in GitHub Desktop.
Save vpadhariya/8c322370db5e335b59dd980a519394c0 to your computer and use it in GitHub Desktop.
Here how we can enable https server for Express Node Server
const express = require('express');
var bodyParser = require('body-parser')
const mongoose = require('mongoose');
const routes = require('./routes/routes');
// for HTTP and HTTPS listening
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('{PATH TO PRIVATE KEY}/privkey.pem', 'utf8');
var certificate = fs.readFileSync('{PATH TO CERTIFICATE}/fullchain.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
// ...
// Your other app settings
app.use(cors())
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json());
// app.use(express.json());
app.use(express.static('public'));
app.use('/', routes);
// ...
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment