Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

express - How to include css and js files in Node.js project

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...