Node.js 应用程序在注册字符串模块上抛出错误 [英] Node.js application throwing error on registring module
问题描述
我正在关注 Steven Sanderson 的视频 此处 开始使用 NodeJS.我已经安装了 EJS
和 ejs-middleware
模块.server.js 如下所示:
I am following Steven Sanderson's videos here to get started with NodeJS. I have installed EJS
and ejs-middleware
modules. The server.js is like following:
var express=require('express'),
app = express(),
ejsMiddleware = require('ejs-middleware');
app.use(ejsMiddleware(__dirname + '/static', 'html', app));
但它正在抛出异常,例如:
But it is throwing exception on this like:
app.use(ejsMiddleware(__dirname + '/static', 'html', app));
例外是:
Application has thrown an uncaught exception and is terminated:
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'register'
at C:\Users\...Inventify\node_modules\ejs-middleware\ejs-middleware.js:9:23
at Object.<anonymous> (C:\Users\...Inventify\server.js:8:9)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object.<anonymous> (C:\Program Files (x86)\iisnode-dev\release\x86\interceptor.js:211:1)
at Module._compile (module.js:446:26)
我无法了解它的任何头部或尾部.请帮帮我.
I'm not able to get any head or tail of it. Please help me.
-根据@Peter Lyons的此答案进行了以下更改:
- Made the following changes according to this answer by @Peter Lyons:
旧
registerInApp.register('.' + extension, ejs);
新
registerInApp.engine('.' + extension, require(ejs));
但现在得到以下异常:
TypeError: Object #<Object> has no method 'substring'
at Function._resolveLookupPaths (module.js:235:23)
at Function._resolveFilename (module.js:327:31)
at Function._load (module.js:279:25)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at C:\Users\...Inventify\node_modules\ejs-middleware\ejs-middleware.js:10:47
at Object.<anonymous> (C:\Users\...Inventify\server.js:12:9)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
推荐答案
看起来需要更新 ejs-middleware
以调用 app.engine
而不是 >app.register
使用 express 3.0.作为一种解决方法,不要将 app
参数传递给 ejsMiddleware 函数,而是自己手动注册它:
It looks like ejs-middleware
needs to be updated to call app.engine
instead of app.register
to work with express 3.0. As a workaround, don't pass the app
argument to the ejsMiddleware function and instead manually register it yourself:
app.engine('.html', require('ejs').renderFile);
我更新了那个.它实际上直接来自 app.engine 的 express.js 文档.据我所知(我个人不使用 ejs),看起来您根本不需要 ejs-middleware
并且可以只使用 visionmedia/ejs
和完成它.
I updated that. It's actually straight from the express.js documentation for app.engine. From what I can tell (I don't use ejs personally), it looks like you don't need ejs-middleware
at all and can just use visionmedia/ejs
and be done with it.
这篇关于Node.js 应用程序在注册字符串模块上抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!