Request parameters not recognized for GET API endpoint download in express

I am trying to make an API endpoint called GET where an application can resolve a file from an id if it is authenticated by the middleware authenticateUser, the problem is that express can not seem to recognize the URL parameter id

I have tried adding the parameter in different files, for more context there is two files, a router.ts file and download.ts

Contents of download.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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 contents of router.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

As previously mentioned, I have tried adding the parameter in different files like this
I tried adding the id parameter in router.ts like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>router.use("/download/:id", download)
</code>
<code>router.use("/download/:id", download) </code>
router.use("/download/:id", download)

Which wasn’t recognized and is seen as undefined
And tried adding it in the download.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>router.get(
"/:id",
authenticateUser,
// Main func here
)
</code>
<code>router.get( "/:id", authenticateUser, // Main func here ) </code>
router.get(
  "/:id",
  authenticateUser,
  // Main func here
)

Reading this question, I have modified the code like this in router.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
)
</code>
<code>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 ) </code>
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
)

But the error still persists stating that the variable is undefined there is no obvious solution I can find.
Here is the full log from the terminal

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterlayer.js:95
fn(req, res, next);
^
TypeError: Cannot read properties of undefined (reading 'fileId')
at D:Websitespicture-upload-download-platformbackendsrcroutesdownload.ts:10:35
at Layer.handle [as handle_request] (D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterlayer.js:95:5)
at next (D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterroute.js:149:13)
at authenticateUser (D:Websitespicture-upload-download-platformbackendsrcutilsauth.ts:37:5)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
</code>
<code>D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterlayer.js:95 fn(req, res, next); ^ TypeError: Cannot read properties of undefined (reading 'fileId') at D:Websitespicture-upload-download-platformbackendsrcroutesdownload.ts:10:35 at Layer.handle [as handle_request] (D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterlayer.js:95:5) at next (D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterroute.js:149:13) at authenticateUser (D:Websitespicture-upload-download-platformbackendsrcutilsauth.ts:37:5) at processTicksAndRejections (node:internal/process/task_queues:95:5) </code>
D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterlayer.js:95
    fn(req, res, next);
    ^
TypeError: Cannot read properties of undefined (reading 'fileId')
    at D:Websitespicture-upload-download-platformbackendsrcroutesdownload.ts:10:35
    at Layer.handle [as handle_request] (D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterlayer.js:95:5)
    at next (D:Websitespicture-upload-download-platformbackendnode_modulesexpresslibrouterroute.js:149:13)
    at authenticateUser (D:Websitespicture-upload-download-platformbackendsrcutilsauth.ts:37:5)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Everything I have tried hasn’t worked. Sorry in advance, I am still new to asking questions so forgive me if the question is not clear or improperly formatted 🙁

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật