0

我在这里关注 Steven Sanderson 的视频以开始使用 NodeJS。我已经安装EJSejs-middleware模块。server.js 如下所示:

var express=require('express'),
app = express(),
ejsMiddleware = require('ejs-middleware');

app.use(ejsMiddleware(__dirname + '/static', 'html', app));

但它会抛出异常,例如:

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)

我无法得到它的任何头部或尾部。请帮我。

编辑:-根据@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)
4

1 回答 1

1

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);

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.

于 2012-12-31T02:40:23.680 回答