在 ejs 的 locals 对象中找到的选项

Options found in locals object in ejs

运行 来自 GitHub 的项目时出现以下错误:"options found in locals object. The option(s) is copied to the option object. This behavior is deprecated and will be removed in EJS 3"

我尝试将 ejs 和 express 模块更新到最新版本,但通知仍然存在。我在 google 上搜索了,ofc,关于它的唯一线索是 this,但它没有帮助。

有人知道更多吗?

作为参考,这里是完整的重要代码:

app/views/index.ejs

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
</head>

<body>
    <h1><%= title %></h1>
    <img src="img/logo.jpg" alt="Hack Hands logo">
</body>
</html>

app/controllers/index.server.controller.js

exports.render = function(req, res) {
    res.render('index', {
        title: 'MEAN MVC'
    });
};

app/routes/index.server.route.js

module.exports = function(app) {
    var index = require('../controllers/index.server.controller');
    app.get('/', index.render);
};

app/config/express.js

var express = require('express');
module.exports = function() {
    var app = express();

    app.set('views', './app/views');
    app.set('view engine', 'ejs');

    require('../app/routes/index.server.routes.js')(app);

    app.use(express.static('./public'));

    return app;
};

server.js

var port = 1337;
var express = require('./config/express');
var app = express();
app.listen(port);
module.exports = app;
console.log('Server running at http://localhost:' + port);

tl;dr:升级到最新版本的 EJS。它删除了所有关于 optionslocals.

的警告

whoami

我是 EJS v2 的合作者(或 上面@micnic 评论中的合作者)。我是在 2.0.3 版(或类似版本)发布后才开始维护 EJS,所以我不太了解 API 更改是如何发生的。

历史

EJS v2 的 renderFile 函数,由 Express.js 使用,现在具有签名

function (path[, options[, locals]], cb)

但为了与 Express.js 兼容,它将所有函数调用为

function (path, locals, cb)

将选项混合到 locals 对象中,EJS 会自动挑选出带有 option-y 名称的局部变量并将它们视为选项。

但是因为Express.js签名也是EJS v1的函数签名,如果locals中的任何选项被复制到options,我们也会打印警告,敦促开发者使用localsoptions 分开的新签名(实际上是 me who added the warning)。

然而,Express.js 用户在调用约定方面没有选择,因此警告始终出现在 Express.js。

确实有一些用户抱怨:#34 #36

起初,@mde(EJS 的主要维护者)推出了 fix,它正确地禁用了 Express.js 上的警告,并且仅在 Express.js 上。

但是,#36 中的人仍然抱怨,因为他正在使用 filename as the name of a local,并且当将可选本地复制到 options 时,会打印警告。

最后,@mde 像 "f*** this shit" 和 removed all the deprecation warnings, including an uncontroversial and legitimate one, and released version 2.2.4 (the legitimate warning was restored by me 发布后一样。

未来

@dougwilson(Express.js 维护者)说他 interested 在 Express.js v5 中 optionslocals 的分离,就像在EJS v2。我确实自愿进行了更改,但是后来我很忙,是的。