Node.js & Express: Static assets on dynamic routes aren't found

Mo Kargas

Bit stumped on how Node.js serves static assets. I'm using the latest Express, EJS, and Node. I have the following route in my app (Some code removed for brevity's sake, there are other routes without parameters). I've searched S.O. for similar questions, but feel my question is possibly different due to having two parameters.

app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
    ...
    app.get('/:userid?/:catid?', function(req, res){
    ... some db queries here ...
        res.render('pages/listing',{
            siteConfig: nconf.get("siteConfig"),
            pageTitle: pageTitle,
            results:dbResults
       });
    }

In my EJS view templates, I have asset paths like the following:

<img src="/images/static_logo.png" />
<script src="/bower_components/modernizr/modernizr.js"></script>
<link rel="stylesheet" href="/css/app.min.css" />

The problem occurs when I pass both id's to this route, let's say localhost/763/201, my static assets appear do not render. All assets begin with the path http://localhost/763/css/style.css (Only the first ID). Ideally I want it to point to http://localhost/css/style.css regardless of the route.

I have tried removing the leading slash in the EJS files, using res.sendfile, and also hard-coding the assets as completely absolute paths, but the first and second options don't work, and the third option is not ideal.

Michael Blankenship

Just noting that my own project's code includes the following without an initial slash before public:

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

It might be worth a try to drop that initial slash in yours and try it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Node.js express routes aren't catching some GETS even with wildcard

From Dev

Node.js/Express static assets connection refused

From Dev

Case sensitivity of Node.js Express static assets

From Dev

Express.js dynamic routes

From Dev

Bootstrap theme's assets management in node.js project with express routes

From Dev

node.js static assets proxy

From Dev

Dynamic routes with Express.js -- is this even possible?

From Dev

How to configure dynamic routes with express.js

From Dev

Node.js express nested routes

From Dev

Node.js express nested routes

From Dev

Node.js + express: specify routes

From Dev

Redirecting routes in Node using Express and Passport JS

From Dev

How do you password protect static assets in Node/Express?

From Dev

Mitigating reflected XSS in node/express requests for static assets

From Dev

Pyramid: static assets not found

From Dev

Node js (Express ) express.static is not working

From Dev

Node js express can't parse body after routes moved into own folder

From Dev

Dynamic Routes not found

From Dev

Static file, stylesheet with Node.js and Express

From Dev

Some assets aren't load in production

From Dev

static site to a dynamic with javascript node.js

From Dev

Node / Express - Serve-static not working / How to use routes with serve-static files

From Dev

node.js express routes using new RegExp

From Dev

How to separate routes on Node.js and Express 4?

From Dev

Node.js and express Rest api to create custom fields routes

From Dev

MVC route matching routes that aren't valid

From Dev

How to draw routes that aren't on roads, MKMapView

From Dev

Aren't all errors caught during compilation regardless of typing (static vs. dynamic) in a compiled language?

From Dev

Routes in express JS

Related Related

  1. 1

    Node.js express routes aren't catching some GETS even with wildcard

  2. 2

    Node.js/Express static assets connection refused

  3. 3

    Case sensitivity of Node.js Express static assets

  4. 4

    Express.js dynamic routes

  5. 5

    Bootstrap theme's assets management in node.js project with express routes

  6. 6

    node.js static assets proxy

  7. 7

    Dynamic routes with Express.js -- is this even possible?

  8. 8

    How to configure dynamic routes with express.js

  9. 9

    Node.js express nested routes

  10. 10

    Node.js express nested routes

  11. 11

    Node.js + express: specify routes

  12. 12

    Redirecting routes in Node using Express and Passport JS

  13. 13

    How do you password protect static assets in Node/Express?

  14. 14

    Mitigating reflected XSS in node/express requests for static assets

  15. 15

    Pyramid: static assets not found

  16. 16

    Node js (Express ) express.static is not working

  17. 17

    Node js express can't parse body after routes moved into own folder

  18. 18

    Dynamic Routes not found

  19. 19

    Static file, stylesheet with Node.js and Express

  20. 20

    Some assets aren't load in production

  21. 21

    static site to a dynamic with javascript node.js

  22. 22

    Node / Express - Serve-static not working / How to use routes with serve-static files

  23. 23

    node.js express routes using new RegExp

  24. 24

    How to separate routes on Node.js and Express 4?

  25. 25

    Node.js and express Rest api to create custom fields routes

  26. 26

    MVC route matching routes that aren't valid

  27. 27

    How to draw routes that aren't on roads, MKMapView

  28. 28

    Aren't all errors caught during compilation regardless of typing (static vs. dynamic) in a compiled language?

  29. 29

    Routes in express JS

HotTag

Archive