Fix routing for Express 5

This commit is contained in:
Asher
2025-03-06 15:03:20 -09:00
parent 50c3e4bb1b
commit 4b7bca38e2
2 changed files with 20 additions and 23 deletions

View File

@ -79,51 +79,48 @@ export const register = async (app: App, args: DefaultedArgs): Promise<Disposabl
app.router.use(common) app.router.use(common)
app.wsRouter.use(common) app.wsRouter.use(common)
app.router.use(async (req, res, next) => { app.router.use(/.*/, async (req, res, next) => {
// If we're handling TLS ensure all requests are redirected to HTTPS. // If we're handling TLS ensure all requests are redirected to HTTPS.
// TODO: This does *NOT* work if you have a base path since to specify the // TODO: This does *NOT* work if you have a base path since to specify the
// protocol we need to specify the whole path. // protocol we need to specify the whole path.
if (args.cert && !(req.connection as tls.TLSSocket).encrypted) { if (args.cert && !(req.connection as tls.TLSSocket).encrypted) {
return res.redirect(`https://${req.headers.host}${req.originalUrl}`) return res.redirect(`https://${req.headers.host}${req.originalUrl}`)
} }
next()
})
// Return security.txt. app.router.get(["/security.txt", "/.well-known/security.txt"], async (_, res) => {
if (req.originalUrl === "/security.txt" || req.originalUrl === "/.well-known/security.txt") {
const resourcePath = path.resolve(rootPath, "src/browser/security.txt") const resourcePath = path.resolve(rootPath, "src/browser/security.txt")
res.set("Content-Type", getMediaMime(resourcePath)) res.set("Content-Type", getMediaMime(resourcePath))
return res.send(await fs.readFile(resourcePath)) res.send(await fs.readFile(resourcePath))
} })
// Return robots.txt. app.router.get("/robots.txt", async (_, res) => {
if (req.originalUrl === "/robots.txt") {
const resourcePath = path.resolve(rootPath, "src/browser/robots.txt") const resourcePath = path.resolve(rootPath, "src/browser/robots.txt")
res.set("Content-Type", getMediaMime(resourcePath)) res.set("Content-Type", getMediaMime(resourcePath))
return res.send(await fs.readFile(resourcePath)) res.send(await fs.readFile(resourcePath))
}
next()
}) })
app.router.use("/", domainProxy.router) app.router.use("/", domainProxy.router)
app.wsRouter.use("/", domainProxy.wsRouter.router) app.wsRouter.use("/", domainProxy.wsRouter.router)
app.router.all("/proxy/:port/:path(.*)?", async (req, res) => { app.router.all("/proxy/:port{/*path}", async (req, res) => {
await pathProxy.proxy(req, res) await pathProxy.proxy(req, res)
}) })
app.wsRouter.get("/proxy/:port/:path(.*)?", async (req) => { app.wsRouter.get("/proxy/:port{/*path}", async (req) => {
await pathProxy.wsProxy(req as WebsocketRequest) await pathProxy.wsProxy(req as unknown as WebsocketRequest)
}) })
// These two routes pass through the path directly. // These two routes pass through the path directly.
// So the proxied app must be aware it is running // So the proxied app must be aware it is running
// under /absproxy/<someport>/ // under /absproxy/<someport>/
app.router.all("/absproxy/:port/:path(.*)?", async (req, res) => { app.router.all("/absproxy/:port{/*path}", async (req, res) => {
await pathProxy.proxy(req, res, { await pathProxy.proxy(req, res, {
passthroughPath: true, passthroughPath: true,
proxyBasePath: args["abs-proxy-base-path"], proxyBasePath: args["abs-proxy-base-path"],
}) })
}) })
app.wsRouter.get("/absproxy/:port/:path(.*)?", async (req) => { app.wsRouter.get("/absproxy/:port{/*path}", async (req) => {
await pathProxy.wsProxy(req as WebsocketRequest, { await pathProxy.wsProxy(req as unknown as WebsocketRequest, {
passthroughPath: true, passthroughPath: true,
proxyBasePath: args["abs-proxy-base-path"], proxyBasePath: args["abs-proxy-base-path"],
}) })

View File

@ -175,7 +175,7 @@ router.get("/manifest.json", async (req, res) => {
const appName = req.args["app-name"] || "code-server" const appName = req.args["app-name"] || "code-server"
res.writeHead(200, { "Content-Type": "application/manifest+json" }) res.writeHead(200, { "Content-Type": "application/manifest+json" })
return res.end( res.end(
replaceTemplates( replaceTemplates(
req, req,
JSON.stringify( JSON.stringify(