Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 7 additions & 5 deletions lib/middleware/redirectWithoutTrailingSlashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down