简体   繁体   中英

Express Node.js Routing Issue

I am building a website using Express and NodeJS. However I am facing this routing issue. This is my route I'm using in my app.js, which is expecting a parameter.

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

And the route will link to:

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

However, upon accessing the link via eg (localhost:2000/purchase/123456), the page loads. However all external files (CSS/JavaScript/JQuery) could not be loaded.

This led me to think that there is something going on with this line of code instead:

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

I tried fiddling with the my routes and I managed to get it completely working with the website loading fine

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

However, I would really like to use the first method I mentioned. Would like to get some help on the above.

Thanks!

I tried it and it works

head.ejs

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

app.js

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

Maybe you're using a relative path like js/jquery.js and not /js/jquery.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM