问题
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));
The exception is:
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.
EDIT:- Made the following changes according to this answer by @Peter Lyons:
old
registerInApp.register('.' + extension, ejs);
new
registerInApp.engine('.' + extension, require(ejs));
But now getting following exception:
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)
回答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.
来源:https://stackoverflow.com/questions/14096085/node-js-application-throwing-error-on-registring-module