Skip to content

Commit

Permalink
nodejs: use let and const instead of var
Browse files Browse the repository at this point in the history
Close #16
  • Loading branch information
hfiguiere committed Sep 26, 2023
1 parent 3ab8cab commit 66eb924
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 54 deletions.
18 changes: 10 additions & 8 deletions webapp/nodejs/app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
'use strict';

var indexRouter = require('./routes/index');
var wopiRouter = require('./routes/wopi');
let express = require('express');
let path = require('path');
let logger = require('morgan');
let bodyParser = require('body-parser');

let indexRouter = require('./routes/index');
let wopiRouter = require('./routes/wopi');

// maximum request body size handled by the bodyParser package
// increase it if you need to handle larger files
var maxDocumentSize = '100kb';
const maxDocumentSize = '100kb';

var app = express();
let app = express();

app.use(logger('dev'));
app.use(express.json());
Expand Down
34 changes: 18 additions & 16 deletions webapp/nodejs/bin/www
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#!/usr/bin/env node

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;
'use strict';

var port = normalizePort(process.env.PORT || '3000');
let app = require('../app');
let debug = require('debug')('untitled:server');
let http = require('http');
let https = require('https');
let fs = require('fs');
let env = require('process').env;

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

var server;
var proto;
let server;
let proto;

var hasCerts = env["SSL_KEY_FILE"] && env["SSL_CRT_FILE"];
let 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"]);
let privateKey = fs.readFileSync(env["SSL_KEY_FILE"]);
let certificate = fs.readFileSync(env["SSL_CRT_FILE"]);

server = https.createServer({
key: privateKey,
Expand All @@ -32,7 +34,7 @@ server.on('error', onError);
server.on('listening', onListening);

function normalizePort(val) {
var port = parseInt(val, 10);
let port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
Expand All @@ -52,7 +54,7 @@ function onError(error) {
throw error;
}

var bind = typeof port === 'string'
let bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

Expand All @@ -75,8 +77,8 @@ function onError(error) {
}

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
let addr = server.address();
let bind = typeof addr === 'string'
? 'pipe ' + addr
: `${proto}://localhost:${addr.port}/`;
console.log(`Listening on ${bind}`);
Expand Down
16 changes: 8 additions & 8 deletions webapp/nodejs/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

<script type="text/javascript">

var locationOrigin = window.location.origin;
var scheme = locationOrigin.startsWith('https') ? 'https' : 'http';
let locationOrigin = window.location.origin;
let scheme = locationOrigin.startsWith('https') ? 'https' : 'http';

$("#collabora-online-server-form").submit(function(event) {
event.preventDefault();
var wopiClientHost = $("#collabora-online-server").val();
let wopiClientHost = $("#collabora-online-server").val();
if (!wopiClientHost) {
alert('No server address entered');
return;
Expand All @@ -45,18 +45,18 @@
alert('Collabora Online server address scheme does not match the current page url scheme');
return;
}
var wopiSrc = locationOrigin + '/wopi/files/1';
let wopiSrc = locationOrigin + '/wopi/files/1';
$.getJSON("/collaboraUrl?server=" + wopiClientHost)
.done(function( resp ) {
var wopiClientUrl = resp['url'];
var accessToken = resp['token'];
var wopiUrl = wopiClientUrl + 'WOPISrc=' + wopiSrc;
let wopiClientUrl = resp['url'];
let accessToken = resp['token'];
let wopiUrl = wopiClientUrl + 'WOPISrc=' + wopiSrc;
$('#collabora-submit-form').attr('action', wopiUrl)
$('#access-token').attr('value', accessToken);
$('#collabora-submit-form').submit();
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
let err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
alert('Not possible to retrieve the complete Collabora Online url');
});
Expand Down
39 changes: 20 additions & 19 deletions webapp/nodejs/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
var express = require('express');
var router = express.Router();
var http = require('http');
var https = require('https');
var Dom = require('@xmldom/xmldom').DOMParser;
var xpath = require('xpath');
'use strict';

let express = require('express');
let router = express.Router();
let http = require('http');
let https = require('https');
let Dom = require('@xmldom/xmldom').DOMParser;
let xpath = require('xpath');


router.get('/', function(req, res) {
res.render('index');
});

router.get('/collaboraUrl', function(req, res) {
var collaboraOnlineHost = req.query.server;
var httpClient = collaboraOnlineHost.startsWith('https') ? https : http;
var data = '';
var request = httpClient.get(collaboraOnlineHost + '/hosting/discovery', function(response) {
let collaboraOnlineHost = req.query.server;
let httpClient = collaboraOnlineHost.startsWith('https') ? https : http;
let data = '';
let request = httpClient.get(collaboraOnlineHost + '/hosting/discovery', function(response) {
response.on('data', function(chunk) { data += chunk.toString(); });
response.on('end', function() {
var err;
if (response.statusCode !== 200) {
err = 'Request failed. Satus Code: ' + response.statusCode;
let err = 'Request failed. Satus Code: ' + response.statusCode;
response.resume();
res.status(response.statusCode).send(err);
console.log(err)
return;
}
if (!response.complete) {
err = 'No able to retrieve the discovery.xml file from the Collabora Online server with the submitted address.';
let err = 'No able to retrieve the discovery.xml file from the Collabora Online server with the submitted address.';
res.status(404).send(err);
console.log(err);
return;
}
var doc = new Dom().parseFromString(data);
let doc = new Dom().parseFromString(data);
if (!doc) {
err = 'The retrieved discovery.xml file is not a valid XML file'
let err = 'The retrieved discovery.xml file is not a valid XML file'
res.status(404).send(err)
console.log(err);
return;
}
var mimeType = 'text/plain';
var nodes = xpath.select("/wopi-discovery/net-zone/app[@name='" + mimeType + "']/action", doc);
let mimeType = 'text/plain';
let nodes = xpath.select("/wopi-discovery/net-zone/app[@name='" + mimeType + "']/action", doc);
if (!nodes || nodes.length !== 1) {
err = 'The requested mime type is not handled'
let err = 'The requested mime type is not handled'
res.status(404).send(err);
console.log(err);
return;
}
var onlineUrl = nodes[0].getAttribute('urlsrc');
let onlineUrl = nodes[0].getAttribute('urlsrc');
res.json({
url: onlineUrl,
token: 'test'
Expand Down
8 changes: 5 additions & 3 deletions webapp/nodejs/routes/wopi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var express = require('express');
var router = express.Router();
'use strict';

let express = require('express');
let router = express.Router();

/* *
* wopi CheckFileInfo endpoint
Expand Down Expand Up @@ -35,7 +37,7 @@ router.get('/files/:fileId/contents', function(req, res) {
// in a real case you should use the file id
// for retrieving the file from the storage and
// send back the file content as response
var fileContent = 'Hello world!';
const fileContent = 'Hello world!';
res.send(fileContent);
});

Expand Down

0 comments on commit 66eb924

Please sign in to comment.