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

后端 未结 1 1912
执笔经年
执笔经年 2020-12-10 06:08

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 wa

相关标签:
1条回答
  • 2020-12-10 06:31

    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

    0 讨论(0)
提交回复
热议问题