I am currently working on setting up an API endpoint named GET
, which allows an application to retrieve a file based on its id after authentication from the middleware authenticateUser
. However, Express is having trouble recognizing the URL parameter id
.
I have attempted adding the parameter in different files. To provide more context, there are two files involved: a router.ts
file and a download.ts
.
Here is the content of download.ts
:
import client from "../config/database"
import { Router, Request, Response } from "express"
import authenticateUser from "../utils/auth"
const router = Router()
router.get(
"/:id",
authenticateUser,
async (res: Response, req: Request) => {
const fileId = req.params.id
const file = await client.query(
"SELECT userid FROM files WHERE id = $1",
[
fileId
]
)
if (file.rows[0].userid == req.user.id) {
res.download("../uploads/" + req.params.id)
return res.status(200).send()
} else {
return res.status(401).send()
}
}
)
export default router
And here is the content of router.ts
:
import { Router } from "express"
import signup from "./signup"
import login from "./login"
import upload from "./upload"
import download from "./download"
import ping from "./ping"
import new_dir from "./newDirectory"
const router = Router()
router.use("/signup", signup)
router.use("/login", login)
router.use("/upload", upload)
router.use("/download/", download)
router.use("/ping", ping)
router.use("/new_dir", new_dir)
export default router
I have tried adding the id
parameter in both router.ts
and download.ts
, but it was not recognized and remained undefined.
router.use("/download/:id", download)
router.get(
"/:id",
authenticateUser,
// Main function here
)
After reviewing a related question on Stack Overflow, I made some modifications to the code in router.ts
:
router.use(
"/download/:id",
(req, res, next) => { // Add a function to assign request params to another variable that can be read by the handler
req.parameters = {}
req.parameters.fileId = 2
next();
},
download
)
Despite these changes, the error persisted indicating that the variable was still undefined
with no clear solution in sight. The following is the error log retrieved from the terminal:
D:\Websites\picture-upload-download-platform\backend\node_modules\express\lib\router\layer.js:95
fn(req, res, next);
^
TypeError: Cannot read properties of undefined (reading 'fileId')
at D:\Websites\picture-upload-download-platform\backend\src\routes\download.ts:10:35
at Layer.handle [as handle_request] (D:\Websites\picture-upload-download-platform\backend\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Websites\picture-upload-download-platform\backend\node_modules\express\lib\router\route.js:149:13)
at authenticateUser (D:\Websites\picture-upload-download-platform\backend\src\utils\auth.ts:37:5)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
Despite all my attempts, I have been unable to resolve this issue. My apologies if the question is unclear or incorrectly formatted, as I am still learning how to ask questions effectively.
EDIT: Below is my authentication middleware used for endpoints like upload
:
const authenticateUser = async (req: Request, res: Response, next: NextFunction) => {
const authToken = req.headers.authorization;
...
};