Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support https #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const https = require('node:https');
const http = require('node:http');
var fs = require('node:fs');
const debug = require('debug')('citizen:server');

const app = require('../app');
Expand All @@ -19,11 +21,29 @@ const normalizePort = (val) => {
return false;
};

const createServer = () => {
if (process.env.SERVER_TYPE === 'https') {
const keyPath = process.env.TLS_KEY_PATH || 'tls/citizen.key'
const crtPath = process.env.TLS_CRT_PATH || 'tls/citizen.crt'
var options = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(crtPath),
};
const port = normalizePort(process.env.PORT || '3443');
console.log(`ListenType: https tlsKeyPath: ${keyPath} tlsCrtPath: ${crtPath} ListenPort: ${port}`)
app.set('port', port);
return { server: https.createServer(options, app), port: port };
} else {
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
console.log(`ListenType: http ListenPort: ${port}`)
return { server: http.createServer(app), port: port };
}
}

const run = () => {
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
const { server, port } = createServer()

const server = http.createServer(app);
server.listen(port);
server.on('error', (error) => {
if (error.syscall !== 'listen') {
Expand Down