我正在关注Steven Sanderson的视频here以开始使用NodeJS。我安装了EJS
和ejs-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根据this answer进行了以下更改:
旧
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)
答案 0 :(得分:1)
看起来ejs-middleware
需要更新才能调用app.engine
而不是app.register
来使用express 3.0。作为一种解决方法,不要将app
参数传递给ejsMiddleware函数,而是自己手动注册:
app.engine('.html', require('ejs').renderFile);
我更新了。它实际上直接来自express.js documentation for app.engine。根据我的判断(我不亲自使用ejs),看起来您根本不需要ejs-middleware
,只需使用visionmedia/ejs
即可完成。