Include files in a .ejs file

Noël Nadal

I am trying to use Express for my Node.js server. I put my web page in /views/page.ejs as required. Yet, I have something like this in my code.

<script type="text/javascript" src="jsfile1.js"></script>
<script type="text/javascript" src="jsfile2.js"></script>
<script type="text/javascript" src="jsfile3.js"></script>
<script type="text/javascript" src="jsfile4.js"></script>

I also have a .css file to load. My question is: how to load them ? I encounter 404 errors. My files are in the same directory as server.js (the Node.js app that I am running).

Thank you for your answers!

Noël.

Dustin Dwyer

You will need to include in your server.js, an express static route to the directory where you want to serve the files.

I have my static assets in /public, So the code that I use to include the static files located in /public is: app.use(express.static(path.join(__dirname, 'public')));

With that static route in place, if you had a file /public/stylesheets/test.css, then you would reference the file like this in your .ejs: <link rel="stylesheet" href="stylesheets/style.css">

Hope that helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related