diff --git a/app.js b/app.js index 700f3137c0..a5d2ee593d 100644 --- a/app.js +++ b/app.js @@ -69,7 +69,9 @@ app.use(morgan('combined', { })) // socket io -var io = require('socket.io')(server) +var io = require('socket.io')(server, config.urlPath ? { + path: `/${config.urlPath}/socket.io` +} : {}) io.engine.ws = new (require('ws').Server)({ noServer: true, perMessageDeflate: false @@ -132,13 +134,24 @@ app.use(cookieParser()) app.use(i18n.init) +// Handle URL path configuration +// Compute URL path prefix (empty string if no urlPath configured) +const urlPathPrefix = config.urlPath ? `/${config.urlPath}` : '' + +// Redirect root to URL path when configured +if (config.urlPath) { + app.get('/', function (req, res) { + res.redirect(301, `${urlPathPrefix}/`) + }) +} + // routes without sessions -// static files -app.use('/', express.static(path.join(__dirname, '/public'), { maxAge: config.staticCacheTime, index: false })) -app.use('/docs', express.static(path.resolve(__dirname, config.docsPath), { maxAge: config.staticCacheTime })) -app.use('/uploads', express.static(path.resolve(__dirname, config.uploadsPath), { maxAge: config.staticCacheTime })) -app.use('/default.md', express.static(path.resolve(__dirname, config.defaultNotePath), { maxAge: config.staticCacheTime })) -app.use(require('./lib/metrics').router) +// static files (mounted with urlPathPrefix, which is '' if no urlPath) +app.use(urlPathPrefix + '/', express.static(path.join(__dirname, '/public'), { maxAge: config.staticCacheTime, index: false })) +app.use(urlPathPrefix + '/docs', express.static(path.resolve(__dirname, config.docsPath), { maxAge: config.staticCacheTime })) +app.use(urlPathPrefix + '/uploads', express.static(path.resolve(__dirname, config.uploadsPath), { maxAge: config.staticCacheTime })) +app.use(urlPathPrefix + '/default.md', express.static(path.resolve(__dirname, config.defaultNotePath), { maxAge: config.staticCacheTime })) +app.use(urlPathPrefix, require('./lib/metrics').router) // session app.use(session({ @@ -227,7 +240,7 @@ app.locals.enableDropBoxSave = config.isDropboxEnable app.locals.enableGitHubGist = config.isGitHubEnable app.locals.enableGitlabSnippets = config.isGitlabSnippetsEnable -app.use(require('./lib/routes').router) +app.use(urlPathPrefix, require('./lib/routes').router) // response not found if no any route matxches app.get('*', function (req, res) { diff --git a/lib/middleware/redirectWithoutTrailingSlashes.js b/lib/middleware/redirectWithoutTrailingSlashes.js index 456e3e300f..ba8981f160 100644 --- a/lib/middleware/redirectWithoutTrailingSlashes.js +++ b/lib/middleware/redirectWithoutTrailingSlashes.js @@ -4,13 +4,15 @@ const config = require('../config') module.exports = function (req, res, next) { if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) { + // Don't redirect if this is the URL path itself (e.g., /codimd/) + if (config.urlPath && req.path === `/${config.urlPath}/`) { + next() + return + } + const queryString = req.url.slice(req.path.length) const urlPath = req.path.slice(0, -1) - let serverURL = config.serverURL - if (config.urlPath) { - serverURL = serverURL.slice(0, -(config.urlPath.length + 1)) - } - res.redirect(301, serverURL + urlPath + queryString) + res.redirect(301, urlPath + queryString) } else { next() }