Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is the correct way to include css and js files in express project ?

I am using ejs template engine. I have seen one example using connect-assetmanager but it was difficult to follow.

A small example project which includes css and js in index.ejs (not layout.ejs) would be very useful.

share|improve this question
Are you also compiling the JS and CSS, or are you just looking to serve static JS and CSS files? – redhotvengeance Jul 29 '12 at 18:31

1 Answer

up vote 4 down vote accepted

Static files can be served with express' static middleware. I usually make a directory for all static files to be served from the root.

If you've installed express globally with npm (npm install -g express) you can type the following at the command line.

express <project_name>

This will create a small example project for you. This example project has a folder named public, from which it serves static files. It further contains folders named javascripts and stylesheets.

The relevant piece of the example project for setting this up is the following line in the file app.js in the function passed to app.configure.

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

Example is from express 3.0.0rc1

Express is built on Connect. The docs for it's static middleware might be helpful: Connect : Static

share|improve this answer
1  
Thanks it worked. Also in the process i learned that express creates project using JADE as default template engine. If you need ejs use the command "express -e <project-name>" – Vinoth Jul 30 '12 at 4:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.