gpt4 book ai didi

javascript - Express Node.js 路由问题

转载 作者:行者123 更新时间:2023-11-30 19:38:46 26 4
gpt4 key购买 nike

我正在使用 Express 和 NodeJS 构建网站。但是我正面临这个路由问题。这是我在 app.js 中使用的路线,它需要一个参数。

app.get(['/purchase/:purchaseID',], getPurchasePage)

路线将链接到:

res.render('purchase.ejs' , {
title: "Purchase Order :" + req.param.referenceID,
referenceID : req.param.referenceID
})

但是,在通过例如访问链接时(localhost:2000/purchase/123456),页面加载。但是无法加载所有外部文件 (CSS/JavaScript/JQuery)。

这让我认为这行代码是有问题的:

app.use(express.static(path.join(__dirname, 'public')));

我试着摆弄我的路线,我设法让它完全与网站加载正常工作

app.get(['/purchase?id=:purchaseID',], getPurchasePage)

但是,我真的很想使用我提到的第一种方法。想在上面得到一些帮助。

谢谢!

最佳答案

我试过了,效果不错

head.ejs

<link rel="stylesheet" href="/js/app.js">

应用程序.js

app.use(express.static(path.join(__dirname, 'public')));

也许你使用的是像 js/jquery.js 而不是/js/jquery.js 这样的相对路径

关于javascript - Express Node.js 路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55644338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com