Express.js 中的 EJS - Express Helpers 不工作 - (app) 在 require 中

发布于 11-30 14:06 字数 996 浏览 1 评论 0原文

我正在将 EJS 模板引擎与express.js 一起使用,并且我想使用视图助手。我在 github 上找到了这个: https://github.com/tanema/express-helpers,但是当我尝试使用它时,我收到此错误:

TypeError: object is not a function
at Object.CALL_NON_FUNCTION (native)
at Object. (/home/node/app/app.js:12:40)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at Array. (module.js:421:10)
at EventEmitter._tickCallback (node.js:126:26)

而这一行是 app.js 上的第 12 行:

var helpers = require('express-helpers')(app);

如果我这样做,

var helpers = require('express-helpers');

它确实会抛出错误,但也不起作用。

我的 app.js 文件顶部如下:

var express = require('express');
var app = express.createServer();
app.listen(34573);

// !Requires
var helpers = require('express-helpers')(app);

有想法吗?

提前致谢!

I'm using the EJS template engine with express.js and I want to use the view helpers. I found this on github: https://github.com/tanema/express-helpers, but when I try to use it I get this error:

TypeError: object is not a function
at Object.CALL_NON_FUNCTION (native)
at Object. (/home/node/app/app.js:12:40)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at Array. (module.js:421:10)
at EventEmitter._tickCallback (node.js:126:26)

whereas this line is line 12 on app.js:

var helpers = require('express-helpers')(app);

and if I make it

var helpers = require('express-helpers');

it does throw the error, but also does not work.

My top of the app.js file is as follows:

var express = require('express');
var app = express.createServer();
app.listen(34573);

// !Requires
var helpers = require('express-helpers')(app);

Ideas?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心凉2024-12-07 14:06:21

我的不好。

事实证明,我的链接是混合的,而我尝试的用法是针对分叉分支的。我克隆了分支,并覆盖了默认的 https://github.com/masahiroh/express-helpers 并且代码按照正确的分支正常工作。

无论如何,谢谢!

My bad.

It turns out that I had my links mixed, and the usage I was trying was meant for a forked branch. I cloned the branch, and overwrote the default https://github.com/masahiroh/express-helpers and the code worked as it was supposed to with the correct branch.

Thanks anyway!

许久2024-12-07 14:06:21

您应该传递给 app.locals() :

var helpers = require('express-helpers')();

app.locals ({
        "form_tag" : helpers.form_tag,
        "form_tag_end" : helpers.form_end_tag,
        "link_to" : helpers.link_to,
        "input_field_tag" : helpers.input_field_tag,
        "submit_tag" : helpers.submit_tag
            ...
        });

You should pass to app.locals() :

var helpers = require('express-helpers')();

app.locals ({
        "form_tag" : helpers.form_tag,
        "form_tag_end" : helpers.form_end_tag,
        "link_to" : helpers.link_to,
        "input_field_tag" : helpers.input_field_tag,
        "submit_tag" : helpers.submit_tag
            ...
        });
机场等船2024-12-07 14:06:21

抱歉,我知道这真的很晚了,但我是您正在谈论的分叉的作者。它是express-helpers 模块的维护版本,应该用于未来的版本。

您遇到的问题是您应该在调用 app.listen() 之前需要帮助程序。 app.listen() 确实应该是文件中的最后一件事,尤其是与应用程序设置有关的任何内容。

Sorry I know this is really late, however I am the author of the fork you are are talking about. It is the maintained version of the express-helpers module and should be used for future versions.

The problem that you were having is that you should be requiring the helpers before you call app.listen(). app.listen() should really be the last thing in your file especially anything concerning app setup.

彩扇题诗2024-12-07 14:06:21

你也可以使用这个 npm 包,它提供了更好的&扩展的帮助者。

https://www.npmjs.com/package/native-view-helpers

npm install native-view-helpers --save

希望它能帮助别人。

You can also use this npm package who provide better & extended helpers.

https://www.npmjs.com/package/native-view-helpers

npm install native-view-helpers --save

Hope it'll help someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文