Skip to content
Merged
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
12 changes: 6 additions & 6 deletions packages/template-mrt-reference-app/app/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ const loggingMiddleware = (req, res, next) => {

const envBasePathMiddleware = (req, res, next) => {
const basePath = process.env.MRT_ENV_BASE_PATH
console.log(`Base path: ${basePath}`)
console.log(`Request path: ${req.url}`)
if (basePath && req.url.startsWith(basePath)) {
req.url = req.path.slice(basePath.length) || '/'
console.log(
`Base path: Rewrote ${basePath} -> Request path: ${req.originalUrl} -> New path: ${req.url}`
console.debug(`Base path: Base path: ${basePath}`)
console.debug(`Request path: Request path: ${req.url}`)
if (basePath && (req.path.startsWith(`${basePath}/`) || req.path === basePath)) {
req.url = req.url.slice(basePath.length) || '/'
console.debug(
`Base path: Rewrote ${basePath} -> Original url: ${req.originalUrl} -> New url: ${req.url}`
)
}
return next()
Expand Down
14 changes: 14 additions & 0 deletions packages/template-mrt-reference-app/app/ssr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ describe('server', () => {
])
}

test('Path /echo should work with base path', async () => {
const basePath = '/test-base-path'
process.env.MRT_ENV_BASE_PATH = basePath
const response = await request(app).get(`${basePath}/echo?x=foo&y=bar`)
expect(response.status).toBe(200)
// preserves query parameters
expect(response.body.query.x).toBe('foo')
expect(response.body.query.y).toBe('bar')
// path is the path after the base path
expect(response.body.path).toBe('/echo')
// base path env var present in response body
expect(response.body.env.MRT_ENV_BASE_PATH).toBe(basePath)
})

beforeEach(() => {
originalEnv = process.env
process.env = Object.assign({}, process.env, {
Expand Down
Loading