简体   繁体   English

如何使用express 3.x在node.js中的EJS模板中包含js文件

[英]How to include js files in my EJS templates in node.js using express 3.x

All the examples I am seeing use the app.dynamicHelpers to provide a method for accessing my javascript files in my view templates. 我看到的所有示例都使用app.dynamicHelpers来提供一种在我的视图模板中访问我的javascript文件的方法。 But in express 3.x there is no clear way to do this. 但在快递3.x中,没有明确的方法可以做到这一点。 The migration docs say "use middleware". 迁移文档说“使用中间件”。 I'm relatively new to the concept of 'middleware' and don't know how to implement this. 我对“中间件”的概念比较陌生,不知道如何实现它。

All I would like to know is the most elegant way to include javascript files in my view templates. 我想知道的是在我的视图模板中包含javascript文件的最优雅方式。

Middleware is the foundation to Connect , which Express is built upon. 中间件是Connect的基础,Express基于此构建。 In short, it allows you to chain together multiple handlers of an incoming HTTP request and response. 简而言之,它允许您将传入的HTTP请求和响应的多个处理程序链接在一起。 The argument you provide to each app.use() in your Express application is basically a "middleware" and is a callback with the following signature function (request, response, next) , where next is the next middleware callback to call in the chain. 您在Express应用程序中为每个app.use()提供的参数基本上是一个“中间件”,并且是一个带有以下签名function (request, response, next)回调,其中next是在链中调用的下一个中间件回调。 All the below are middleware (the last being an error handler which has a 4 parameter signature): 以下所有都是中间件(最后一个是具有4个参数签名的错误处理程序 ):

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(function(err, req, res, next){
  // logic
  next();
});

In terms of the move from Express 2 to 3.x, the migration is specifically: 就从Express 2迁移到3.x而言,迁移具体为:

app.dynamicHelpers( ) (use middleware + res.locals ) app.dynamicHelpers( )(使用中间件+ res.locals

So, with a contrived example, where you may have previously had a helper function in an external js file and require d it for use in your dynamicHelper : 因此,有一个人为的例子,你可能以前在外部js文件中有一个辅助函数,并且requiredynamicHelper使用它:

//helpers.js
exports.dynamicHelpers = {
  currentUser: function(req, res) {
    return req.user;
  }
};

// app.js
var helpers = require('./helpers').dynamicHelpers;

app.dynamicHelpers(helpers);

With a bit of reorganization you could now do something like: 通过一些重组,您现在可以执行以下操作:

//locals.js
exports.setLocals = function(req, res, next){ //<- middleware function
  res.locals.currentUser = req.user;
  res.locals.otherVariable = ...;
  next();
}

//app.js
var locals = require('./locals').setLocals;
...
app.use(locals);

This is a good blog with a review of middleware vs. dynamicHelpers in pre-3.x Express, but the concepts are the same. 这是一篇很好的博客,其中包含3.x Express之前的中间件与dynamicHelpers的评论,但概念是相同的。 The only difference being that res.local(name, value) used in the article is now deprecated for res.locals.name = value or res.locals({ name: value }) 唯一的区别是文章中使用的res.local(name, value)现在已弃用 res.locals.name = valueres.locals({ name: value })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM