aboutsummaryrefslogtreecommitdiffhomepage
path: root/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server.js')
-rwxr-xr-xserver.js21
1 files changed, 11 insertions, 10 deletions
diff --git a/server.js b/server.js
index f661e87..6206103 100755
--- a/server.js
+++ b/server.js
@@ -22,11 +22,11 @@ var express = require('express'),
22 files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files')); 22 files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files'));
23 23
24 24
25var rootFolder = path.resolve(__dirname, process.argv[2] || 'files'); 25const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files');
26var configFile = path.resolve(__dirname, process.argv[3] || '.config.json'); 26const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json');
27 27
28// Ensure the root folder exists 28// Ensure the root folder exists
29mkdirp.sync(rootFolder); 29mkdirp.sync(ROOT_FOLDER);
30 30
31var config = { 31var config = {
32 folderListingEnabled: false 32 folderListingEnabled: false
@@ -41,7 +41,7 @@ function setSettings(req, res, next) {
41 41
42 config.folderListingEnabled = !!req.body.folderListingEnabled; 42 config.folderListingEnabled = !!req.body.folderListingEnabled;
43 43
44 fs.writeFile(configFile, JSON.stringify(config), function (error) { 44 fs.writeFile(CONFIG_FILE, JSON.stringify(config), function (error) {
45 if (error) return next(new HttpError(500, 'unable to save settings')); 45 if (error) return next(new HttpError(500, 'unable to save settings'));
46 46
47 next(new HttpSuccess(201, {})); 47 next(new HttpSuccess(201, {}));
@@ -50,10 +50,11 @@ function setSettings(req, res, next) {
50 50
51// Load the config file 51// Load the config file
52try { 52try {
53 config = require(configFile); 53 console.log(`Using config file: ${CONFIG_FILE}`);
54 config = require(CONFIG_FILE);
54} catch (e) { 55} catch (e) {
55 if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${configFile} not found`); 56 if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${CONFIG_FILE} not found`);
56 else console.log(`Cannot load config file ${configFile}`, e); 57 else console.log(`Cannot load config file ${CONFIG_FILE}`, e);
57} 58}
58 59
59if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; 60if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true;
@@ -85,7 +86,7 @@ app.use('/api', passport.initialize());
85app.use('/api', passport.session()); 86app.use('/api', passport.session());
86app.use(router); 87app.use(router);
87app.use('/_admin', express.static(__dirname + '/frontend')); 88app.use('/_admin', express.static(__dirname + '/frontend'));
88app.use('/', express.static(rootFolder)); 89app.use('/', express.static(ROOT_FOLDER));
89app.use('/', function welcomePage(req, res, next) { 90app.use('/', function welcomePage(req, res, next) {
90 if (config.folderListingEnabled || req.path !== '/') return next(); 91 if (config.folderListingEnabled || req.path !== '/') return next();
91 res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html')); 92 res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html'));
@@ -94,7 +95,7 @@ app.use('/', function (req, res, next) {
94 if (config.folderListingEnabled) return next(); 95 if (config.folderListingEnabled) return next();
95 res.sendFile(__dirname + '/frontend/404.html'); 96 res.sendFile(__dirname + '/frontend/404.html');
96}); 97});
97app.use('/', serveIndex(rootFolder, { icons: true })); 98app.use('/', serveIndex(ROOT_FOLDER, { icons: true }));
98app.use(lastMile()); 99app.use(lastMile());
99 100
100var server = app.listen(3000, function () { 101var server = app.listen(3000, function () {
@@ -102,5 +103,5 @@ var server = app.listen(3000, function () {
102 var port = server.address().port; 103 var port = server.address().port;
103 104
104 console.log('Surfer listening on http://%s:%s', host, port); 105 console.log('Surfer listening on http://%s:%s', host, port);
105 console.log('Using base path', rootFolder); 106 console.log('Using base path', ROOT_FOLDER);
106}); 107});