Skip to content

Commit 4e24b74

Browse files
committed
feat: implement URL path configuration for static files and routes
- Added support for a configurable URL path in the application, allowing static files and routes to be served under a specified path. - Implemented redirection from the root to the configured URL path. - Updated socket.io initialization to respect the URL path configuration. - Enhanced middleware to prevent redirection for the URL path itself. This change improves flexibility in routing and enhances the application's structure. Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
1 parent 496d955 commit 4e24b74

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

app.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ app.use(morgan('combined', {
6969
}))
7070

7171
// socket io
72-
var io = require('socket.io')(server)
72+
var io = require('socket.io')(server, config.urlPath ? {
73+
path: `/${config.urlPath}/socket.io`
74+
} : {})
7375
io.engine.ws = new (require('ws').Server)({
7476
noServer: true,
7577
perMessageDeflate: false
@@ -132,13 +134,24 @@ app.use(cookieParser())
132134

133135
app.use(i18n.init)
134136

137+
// Handle URL path configuration
138+
// Compute URL path prefix (empty string if no urlPath configured)
139+
const urlPathPrefix = config.urlPath ? `/${config.urlPath}` : ''
140+
141+
// Redirect root to URL path when configured
142+
if (config.urlPath) {
143+
app.get('/', function (req, res) {
144+
res.redirect(301, `${urlPathPrefix}/`)
145+
})
146+
}
147+
135148
// routes without sessions
136-
// static files
137-
app.use('/', express.static(path.join(__dirname, '/public'), { maxAge: config.staticCacheTime, index: false }))
138-
app.use('/docs', express.static(path.resolve(__dirname, config.docsPath), { maxAge: config.staticCacheTime }))
139-
app.use('/uploads', express.static(path.resolve(__dirname, config.uploadsPath), { maxAge: config.staticCacheTime }))
140-
app.use('/default.md', express.static(path.resolve(__dirname, config.defaultNotePath), { maxAge: config.staticCacheTime }))
141-
app.use(require('./lib/metrics').router)
149+
// static files (mounted with urlPathPrefix, which is '' if no urlPath)
150+
app.use(urlPathPrefix + '/', express.static(path.join(__dirname, '/public'), { maxAge: config.staticCacheTime, index: false }))
151+
app.use(urlPathPrefix + '/docs', express.static(path.resolve(__dirname, config.docsPath), { maxAge: config.staticCacheTime }))
152+
app.use(urlPathPrefix + '/uploads', express.static(path.resolve(__dirname, config.uploadsPath), { maxAge: config.staticCacheTime }))
153+
app.use(urlPathPrefix + '/default.md', express.static(path.resolve(__dirname, config.defaultNotePath), { maxAge: config.staticCacheTime }))
154+
app.use(urlPathPrefix, require('./lib/metrics').router)
142155

143156
// session
144157
app.use(session({
@@ -227,7 +240,7 @@ app.locals.enableDropBoxSave = config.isDropboxEnable
227240
app.locals.enableGitHubGist = config.isGitHubEnable
228241
app.locals.enableGitlabSnippets = config.isGitlabSnippetsEnable
229242

230-
app.use(require('./lib/routes').router)
243+
app.use(urlPathPrefix, require('./lib/routes').router)
231244

232245
// response not found if no any route matxches
233246
app.get('*', function (req, res) {

lib/middleware/redirectWithoutTrailingSlashes.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ const config = require('../config')
44

55
module.exports = function (req, res, next) {
66
if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) {
7+
// Don't redirect if this is the URL path itself (e.g., /codimd/)
8+
if (config.urlPath && req.path === `/${config.urlPath}/`) {
9+
next()
10+
return
11+
}
12+
713
const queryString = req.url.slice(req.path.length)
814
const urlPath = req.path.slice(0, -1)
9-
let serverURL = config.serverURL
10-
if (config.urlPath) {
11-
serverURL = serverURL.slice(0, -(config.urlPath.length + 1))
12-
}
13-
res.redirect(301, serverURL + urlPath + queryString)
15+
res.redirect(301, urlPath + queryString)
1416
} else {
1517
next()
1618
}

0 commit comments

Comments
 (0)