如何在Node.js项目中包含css和js文件 [英] How to include css and js files in Node.js project
问题描述
我正在使用ejs模板引擎。我看到一个使用connect-assetmanager的例子,但是很难遵循。
一个小例子项目,其中包括index.ejs中的css和js(而不是layout.ejs)将非常有用。
静态文件可以用express的静态中间件提供。我通常会从根目录提供所有静态文件的目录。
如果您已经在全球安装了npm( npm install - g express
),您可以在命令行中键入以下内容。
express< project_name>
这将为您创建一个小型示例项目。此示例项目具有名为 public
的文件夹,从该文件夹中提供静态文件。它还包含名为 javascripts
和样式表
的文件夹。
在设置此项目的示例项目的相关部分是文件 app.js
中的以下行传递到 app.configure
。
app.use(express.static(path_join(__ dirname,'public'))) ;
示例来自express 3.0.0rc1
Express是建立在Connect上的。静态中间件的文档可能会有所帮助:连接:静态
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.
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
这篇关于如何在Node.js项目中包含css和js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!