diff options
Diffstat (limited to 'server.js')
-rwxr-xr-x | server.js | 21 |
1 files changed, 11 insertions, 10 deletions
@@ -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 | ||
25 | var rootFolder = path.resolve(__dirname, process.argv[2] || 'files'); | 25 | const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files'); |
26 | var configFile = path.resolve(__dirname, process.argv[3] || '.config.json'); | 26 | const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json'); |
27 | 27 | ||
28 | // Ensure the root folder exists | 28 | // Ensure the root folder exists |
29 | mkdirp.sync(rootFolder); | 29 | mkdirp.sync(ROOT_FOLDER); |
30 | 30 | ||
31 | var config = { | 31 | var 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 |
52 | try { | 52 | try { |
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 | ||
59 | if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; | 60 | if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; |
@@ -85,7 +86,7 @@ app.use('/api', passport.initialize()); | |||
85 | app.use('/api', passport.session()); | 86 | app.use('/api', passport.session()); |
86 | app.use(router); | 87 | app.use(router); |
87 | app.use('/_admin', express.static(__dirname + '/frontend')); | 88 | app.use('/_admin', express.static(__dirname + '/frontend')); |
88 | app.use('/', express.static(rootFolder)); | 89 | app.use('/', express.static(ROOT_FOLDER)); |
89 | app.use('/', function welcomePage(req, res, next) { | 90 | app.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 | }); |
97 | app.use('/', serveIndex(rootFolder, { icons: true })); | 98 | app.use('/', serveIndex(ROOT_FOLDER, { icons: true })); |
98 | app.use(lastMile()); | 99 | app.use(lastMile()); |
99 | 100 | ||
100 | var server = app.listen(3000, function () { | 101 | var 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 | }); |