mirror of
https://github.com/coder/code-server.git
synced 2025-09-18 10:32:05 +08:00
Fix routing for Express 5
This commit is contained in:
@ -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}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return security.txt.
|
|
||||||
if (req.originalUrl === "/security.txt" || req.originalUrl === "/.well-known/security.txt") {
|
|
||||||
const resourcePath = path.resolve(rootPath, "src/browser/security.txt")
|
|
||||||
res.set("Content-Type", getMediaMime(resourcePath))
|
|
||||||
return res.send(await fs.readFile(resourcePath))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return robots.txt.
|
|
||||||
if (req.originalUrl === "/robots.txt") {
|
|
||||||
const resourcePath = path.resolve(rootPath, "src/browser/robots.txt")
|
|
||||||
res.set("Content-Type", getMediaMime(resourcePath))
|
|
||||||
return res.send(await fs.readFile(resourcePath))
|
|
||||||
}
|
|
||||||
|
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.router.get(["/security.txt", "/.well-known/security.txt"], async (_, res) => {
|
||||||
|
const resourcePath = path.resolve(rootPath, "src/browser/security.txt")
|
||||||
|
res.set("Content-Type", getMediaMime(resourcePath))
|
||||||
|
res.send(await fs.readFile(resourcePath))
|
||||||
|
})
|
||||||
|
|
||||||
|
app.router.get("/robots.txt", async (_, res) => {
|
||||||
|
const resourcePath = path.resolve(rootPath, "src/browser/robots.txt")
|
||||||
|
res.set("Content-Type", getMediaMime(resourcePath))
|
||||||
|
res.send(await fs.readFile(resourcePath))
|
||||||
|
})
|
||||||
|
|
||||||
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"],
|
||||||
})
|
})
|
||||||
|
@ -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(
|
||||||
|
Reference in New Issue
Block a user