1
0
mirror of synced 2025-12-23 03:44:00 -05:00
Files
docs/middleware/anchor-redirect.js
2023-02-28 22:24:38 +00:00

30 lines
923 B
JavaScript

import express from 'express'
import path from 'path'
import { readCompressedJsonFileFallbackLazily } from '../lib/read-json-file.js'
import { defaultCacheControl } from './cache-control.js'
import { REST_DATA_DIR } from '../src/rest/lib/index.js'
const clientSideRestAPIRedirects = readCompressedJsonFileFallbackLazily(
path.join(REST_DATA_DIR, 'client-side-rest-api-redirects.json')
)
const router = express.Router()
// Returns a client side redirect if one exists for the given path.
router.get('/', function redirects(req, res) {
if (!req.query.path) {
return res.status(400).send("Missing 'path' query string")
}
if (!req.query.hash) {
return res.status(400).send("Missing 'hash' query string")
}
defaultCacheControl(res)
const redirectFrom = `${req.query.path}#${req.query.hash}`
res.status(200).send({ to: clientSideRestAPIRedirects()[redirectFrom] } || null)
})
export default router