Sunday, 16 April 2017

Node JS, EJS include actions

I am writing an admin dashboard in Node JS.The HTML views are being created using EJS. Below is an example of how my folders are set up.

/root
    app.js
    /models
    /node_modules
    package.json
    /public
        /css
            style.css
        /fonts
        /images
        /js
        /pages
        /plugins
        /scss
    /views
        /admin
            dashboard.ejs
        error.ejs
        index.ejs
        /partials
            header.ejs
        /user

In my app.js file I created a route to the admin dashboard after a user logs in using:

app.get('/admindashboard', function(req,res){})

In my dashboard.ejs file I include the header.ejs file using,

<% include ../partials/header %>

Essentially, my header.ejs file contains some CSS where they are linked in this formatt

<link rel="stylesheet" href="plugins/morris/morris.css">

Running on my Localhost, this worked and rendered the page correctly.

I then wanted to add a bit more information into the URL, using an id parameter that coincides with req.session.id value, within my GET request:

app.get('/admindashboard/:id', function(req,res){})

Now I am receiving an error in my browser console that my "plugins/morris/morris.css" can't be found.

My question is, since I added more to the URL, how do I need to adjust the CSS reference to reflect the change?



via Michael Westbrooks II

No comments:

Post a Comment