Skip to content

Commit

Permalink
nodejs: Add https support for both nodejs samples
Browse files Browse the repository at this point in the history
- launching the demo server will printout the proper URL to use
- https support is automatic if the environment is setup.

Issue CollaboraOnline#14
  • Loading branch information
hfiguiere committed Sep 25, 2023
1 parent 8e607a3 commit 32a9089
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
28 changes: 27 additions & 1 deletion webapp/nodejs-esm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ We assume you are familiar with npm and the node.js framework.

1. In order to install dependencies run `npm intall` in the project folder.
2. In the console run the `npm start` command for starting the server.
3. Start your browser and make it point to the url `http://<host>:3000`
3. Start your browser and make it point to the url
`http://<host>:3000` or `https://<host>:3000` depending on whether
you setup certifcates (see below).

To make it reachable by the Collabora Online server use as `<host>`
the IP address of the machine where the NodeJS server is
Expand All @@ -34,6 +36,30 @@ We assume you are familiar with npm and the node.js framework.
* ` Hello World! Hi!` - the updated file content has been
successfully received

### Certificates

It is highly recommended to setup TLS certificates for https.

If you don't have a key pair, I recommend using
[minica](https://github.com/jsha/minica) to generate a self-signed
one.

**THIS IS ONLY FOR TEST AND DEVELOPMENT. NEVER USE SELF SIGNED
CERTIFICATE IN A PRODUCTION ENVIRONMENT**

Then set the environment to indicate where to load the certificate from.

- `SSL_KEY_FILE` contains the path to the private key. If you used
the `minica` tool mentionned above, it's the path to the
`minica-key.pem` file.
- `SSL_CRT_FILE` contains the path to the public certificate. If you used
the `minica` tool mentionned above, it's the path to the
`minica.pem` file.

To use self-signed certificate, NodeJS requires to set the environment
`NODE_TLS_REJECT_UNAUTHORIZED='0'`, otherwise it will throw a
`'SELF_SIGNED_CERT_IN_CHAIN'` error.

## Note

By default the [body-parser][] node.js package used as middleware for
Expand Down
25 changes: 22 additions & 3 deletions webapp/nodejs-esm/bin/www.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
#!/usr/bin/env node


import app from '../app.js';
import debug from 'debug';
debug('untitled:server');
import fs from "fs";
import http from 'http';
import https from 'https';
import {env} from 'process';

let port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

let server = http.createServer(app);
let server;
let proto;

let hasCerts = env["SSL_KEY_FILE"] && env["SSL_CRT_FILE"];
if (hasCerts) {
let privateKey = fs.readFileSync(env["SSL_KEY_FILE"]);
let certificate = fs.readFileSync(env["SSL_CRT_FILE"]);

server = https.createServer({
key: privateKey,
cert: certificate
}, app);
proto = 'https';
} else {
server = http.createServer(app);
proto = 'http';
}
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
Expand Down Expand Up @@ -61,6 +80,6 @@ function onListening() {
let addr = server.address();
let bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
: `${proto}://localhost:${addr.port}/`;
console.log(`Listening on ${bind}`);
}
31 changes: 29 additions & 2 deletions webapp/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ We assume you are familiar with npm and the node.js framework.

1. In order to install dependencies run `npm intall` in the project folder.
2. In the console run the `npm start` command for starting the server.
3. Start your browser and make it point to the url `http://<host>:3000`

3. Start your browser and make it point to the url
`http://<host>:3000` or `https://<host>:3000` depending on whether
you setup certifcates (see below).

To make it reachable by the Collabora Online server use as `<host>` the IP address of the machine where the NodeJS
server is running. In case the NodeJs server can't be reached you could also need to open the port 3000 on the firewall.

Expand All @@ -27,6 +29,31 @@ We assume you are familiar with npm and the node.js framework.
* `wopi PutFile endpoint` - the PutFile wopi endpoint has been triggered
* ` Hello World! Hi!` - the updated file content has been successfully received


### Certificates

It is highly recommended to setup TLS certificates for https.

If you don't have a key pair, I recommend using
[minica](https://github.com/jsha/minica) to generate a self-signed
one.

**THIS IS ONLY FOR TEST AND DEVELOPMENT. NEVER USE SELF SIGNED
CERTIFICATE IN A PRODUCTION ENVIRONMENT**

Then set the environment to indicate where to load the certificate from.

- `SSL_KEY_FILE` contains the path to the private key. If you used
the `minica` tool mentionned above, it's the path to the
`minica-key.pem` file.
- `SSL_CRT_FILE` contains the path to the public certificate. If you used
the `minica` tool mentionned above, it's the path to the
`minica.pem` file.

To use self-signed certificate, NodeJS requires to set the environment
`NODE_TLS_REJECT_UNAUTHORIZED='0'`, otherwise it will throw a
`'SELF_SIGNED_CERT_IN_CHAIN'` error.

## Note

By default the [body-parser][] node.js package used as middleware for the `PutFile` endpoint has a limit option which
Expand Down
24 changes: 21 additions & 3 deletions webapp/nodejs/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@
var app = require('../app');
var debug = require('debug')('untitled:server');
var http = require('http');
var https = require('https');
var fs = require('fs');
var env = require('process').env;

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

var server = http.createServer(app);
var server;
var proto;

var hasCerts = env["SSL_KEY_FILE"] && env["SSL_CRT_FILE"];
if (hasCerts) {
var privateKey = fs.readFileSync(env["SSL_KEY_FILE"]);
var certificate = fs.readFileSync(env["SSL_CRT_FILE"]);

server = https.createServer({
key: privateKey,
cert: certificate
}, app);
proto = 'https';
} else {
server = http.createServer(app);
proto = 'http';
}
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
Expand Down Expand Up @@ -60,6 +78,6 @@ function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
: `${proto}://localhost:${addr.port}/`;
console.log(`Listening on ${bind}`);
}

0 comments on commit 32a9089

Please sign in to comment.