diff --git a/webapp/nodejs-esm/README.md b/webapp/nodejs-esm/README.md index 6eb70a3..3152c04 100644 --- a/webapp/nodejs-esm/README.md +++ b/webapp/nodejs-esm/README.md @@ -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://:3000` +3. Start your browser and make it point to the url + `http://:3000` or `https://:3000` depending on whether + you setup certifcates (see below). To make it reachable by the Collabora Online server use as `` the IP address of the machine where the NodeJS server is @@ -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 diff --git a/webapp/nodejs-esm/bin/www.js b/webapp/nodejs-esm/bin/www.js index c4f2bdd..0379255 100755 --- a/webapp/nodejs-esm/bin/www.js +++ b/webapp/nodejs-esm/bin/www.js @@ -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); @@ -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}`); } diff --git a/webapp/nodejs/README.md b/webapp/nodejs/README.md index 1e0bd95..4391baa 100644 --- a/webapp/nodejs/README.md +++ b/webapp/nodejs/README.md @@ -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://:3000` - +3. Start your browser and make it point to the url + `http://:3000` or `https://:3000` depending on whether + you setup certifcates (see below). + To make it reachable by the Collabora Online server use as `` 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. @@ -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 diff --git a/webapp/nodejs/bin/www b/webapp/nodejs/bin/www index 04dfb74..88b7e3b 100755 --- a/webapp/nodejs/bin/www +++ b/webapp/nodejs/bin/www @@ -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); @@ -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}`); }