Node.js application throwing error on registring module - node.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));
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)

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.

Related

firebase-fuctions ,TypeError: Cannot read property 'onRequest' of undefined

updated firebase via npm i -g firebase-tools ,
used my firebase id ,
while deploying my function at firebase ( with firebase deploy ) this error results out
Error: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'onRequest' of undefined
at Object.<anonymous> (C:\Users\umer\Desktop\learning chatbots\usingFulfilmentAsWebhook\functions\index.js:9:34)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\umer\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:15:15
at Object.<anonymous> (C:\Users\umer\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:53:3)
here is the code
const functions = require('firebase-functions');
exports.webhook = functions.http.onRequest((request,response)=>{
console.log("request.body.result.parameters", request.body.result.parameters);
let params = request.body.result.parameters;
response.send({
speech: `${params.name} your hotel booking request for ${params.roomType} room is forwarded for ${params.persons} persons. We will contact you on ${params.email} soon`
});
});
try after putting "s" in http in below line
exports.webhook = functions.https.onRequest((request,response)=>{
...
}
Add this in your first line
const functions = require('firebase-functions');
Besides, make sure you have necessary packages installed.

Why does Handlebars.template(templateSpec) not work as (I) expected?

I'm trying to precompile a handlebar template in a node app. Later, I would like to store/retrieve the precompiled template to/from a database and use it.
var handlebars = require('handlebars');
var templateSpec = handlebars.precompile('{{foo}}');
var template = handlebars.template(templateSpec);
I base this code on the Handlebars reference.
The code above throws errors:
/home/ubuntu/workspace/handlebars/node_modules/handlebars/dist/cjs/handlebars/runtime.js:50
throw new _exception2['default']('Unknown template object: ' + typeof templateSpec);
^
Error: Unknown template object: string
at Object.template (/home/ubuntu/workspace/handlebars/node_modules/handlebars/dist/cjs/handlebars/runtime.js:50:11)
at HandlebarsEnvironment.hb.template (/home/ubuntu/workspace/handlebars/node_modules/handlebars/dist/cjs/handlebars.runtime.js:51:20)
at Object.<anonymous> (/home/ubuntu/workspace/handlebars/test.js:5:27)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:990:3
Not sure what I'm doing wrong.

Main Bower files

In my angularjs app, i try to concat bower file because i have to many lib. I make a task for concat file, here is code
var filter = require('gulp-filter');
var mainBowerFiles = require('gulp-main-bower-files');
var dest = 'dist/scripts';
gulp.task('main-bower-files', function() {
return gulp.src('./bower.json')
.pipe(mainBowerFiles([[filter, ]options][, callback]))
.pipe(gulp.dest('dist/scripts'));
});
After i call this task in nodejs cmd, i get this error in chrome console
.pipe(mainBowerFiles([[filter, ]options][, callback]))
This is error in node cmd
.pipe(mainBowerFiles([[filter, ]options][, callback]))
^^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Liftoff.handleArguments (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:116:3)
at Liftoff.<anonymous> (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:198:16)
at module.exports (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
If someone know solution??? Thnx
i fix this with another library, called BOWER-FILES.
var lib = require('bower-files')();
gulp.task('bower_js', function () {
gulp.src(jsBowerFile)
.pipe(concat('bower_js.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(ngmin())
.pipe(uglify({mangle: false}))
.pipe(gulp.dest('dist/scripts'));
});

Function and module.exports error issue NODE.js

:)
I have a somewhat easy answer for you guys to answer as you always do.
Im new at functions and whatnot, iv watched some tutorials about exporting your functions to another node.js application,
Im attempting to generate some random numbers for a external module.
this is what i have setup.
(index.js file)
function randNumb(topnumber) {
var randnumber=Math.floor(Math.random()*topnumber)
}
module.exports.randNumb();
(run.js)
var index = require("./run.js");
console.log(randnumber);
Well My Issue is when i run the index.js file, i get this error from the console.
TypeError: Object #<Object> has no method 'randNumb'
at Object.<anonymous> (C:\Users\Christopher Allen\Desktop\Node Dev Essential
s - Random Number\index.js:8:16)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
I ran the run.js in the beginning, this is what i got.
ReferenceError: randNumb is not defined
at Object.<anonymous> (C:\Users\Christopher Allen\Desktop\Node Dev Essential
s - Random Number\run.js:3:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
In the randNum function, don't forget to:
return randnumber;
Also in index.js, export the function like so:
exports.randNumb = randNumb;
Invoke it like this in run.js:
console.log(randNumber(10));
You didn't export the var at all.
Your index.js should be :
function randNumb(topnumber) {
return Math.floor(Math.random()*topnumber)
}
module.exports.randnumber = randNumb(10); //replace 10 with any other number...
run.js should be :
var index = require("./run.js");
console.log(index.randnumber);

TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'mapDefaultAction' with nodejs

I was getting error with express-resource so I have applied patch as mentioned in following link.
As mentioned in following question at stack overflow:
express-namespace and express-resource wont allow map
I employed following command:
npm install git://github.com/visionmedia/express-resource.git#add/express3x-support
but now I am getting following error:
C:\Data\work\node\example\www\example\node_modules\express-resource\index.js:63
if (actions[key]) this.mapDefaultAction(key, actions[key]);
TypeError: Object function app(req, res){ app.handle(req, res); } has no method
'mapDefaultAction'
at Function.Resource (C:\Data\work\node\example\www\example\node_modules\
express-resource\index.js:63:28)
at Object.<anonymous> (C:\Data\work\node\example\www\mixerade\app.js:23:17)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
can anyone please suggest what could be the possible reason?
Thanks in advance.

Resources