@@ -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+ } : { } )
7375io . engine . ws = new ( require ( 'ws' ) . Server ) ( {
7476 noServer : true ,
7577 perMessageDeflate : false
@@ -132,13 +134,29 @@ app.use(cookieParser())
132134
133135app . use ( i18n . init )
134136
135- // 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 )
137+ // Handle URL path configuration
138+ if ( config . urlPath ) {
139+ // Redirect from root to URL path (with trailing slash)
140+ app . get ( '/' , function ( req , res ) {
141+ res . redirect ( 301 , `/${ config . urlPath } /` )
142+ } )
143+
144+ // Mount static files and routes under URL path
145+ const urlPathPrefix = `/${ config . urlPath } `
146+ app . use ( urlPathPrefix + '/' , express . static ( path . join ( __dirname , '/public' ) , { maxAge : config . staticCacheTime , index : false } ) )
147+ app . use ( urlPathPrefix + '/docs' , express . static ( path . resolve ( __dirname , config . docsPath ) , { maxAge : config . staticCacheTime } ) )
148+ app . use ( urlPathPrefix + '/uploads' , express . static ( path . resolve ( __dirname , config . uploadsPath ) , { maxAge : config . staticCacheTime } ) )
149+ app . use ( urlPathPrefix + '/default.md' , express . static ( path . resolve ( __dirname , config . defaultNotePath ) , { maxAge : config . staticCacheTime } ) )
150+ app . use ( urlPathPrefix , require ( './lib/metrics' ) . router )
151+ } else {
152+ // routes without sessions
153+ // static files
154+ app . use ( '/' , express . static ( path . join ( __dirname , '/public' ) , { maxAge : config . staticCacheTime , index : false } ) )
155+ app . use ( '/docs' , express . static ( path . resolve ( __dirname , config . docsPath ) , { maxAge : config . staticCacheTime } ) )
156+ app . use ( '/uploads' , express . static ( path . resolve ( __dirname , config . uploadsPath ) , { maxAge : config . staticCacheTime } ) )
157+ app . use ( '/default.md' , express . static ( path . resolve ( __dirname , config . defaultNotePath ) , { maxAge : config . staticCacheTime } ) )
158+ app . use ( require ( './lib/metrics' ) . router )
159+ }
142160
143161// session
144162app . use ( session ( {
@@ -227,7 +245,11 @@ app.locals.enableDropBoxSave = config.isDropboxEnable
227245app . locals . enableGitHubGist = config . isGitHubEnable
228246app . locals . enableGitlabSnippets = config . isGitlabSnippetsEnable
229247
230- app . use ( require ( './lib/routes' ) . router )
248+ if ( config . urlPath ) {
249+ app . use ( `/${ config . urlPath } ` , require ( './lib/routes' ) . router )
250+ } else {
251+ app . use ( require ( './lib/routes' ) . router )
252+ }
231253
232254// response not found if no any route matxches
233255app . get ( '*' , function ( req , res ) {
0 commit comments