javascript - Calling helper function in node.js within callback? -
i'm new programming node.js , not quite sure why getting error. function looks set correctly, , don't believe have asynchronous problems b/c should account self variable put in place (i think). did try w/o too, using simple var consoleprint(...) anyways, code below , error log below that.
/* global __dirname */ var express = require('express'); var app = express(); var bodyparser = require('body-parser'); var self = this; //calling helper function here var server = app.listen(8000, self.consoleprint(server)); app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.json()); app.use('/public', express.static(__dirname + '/public')); app.get('/', function (req, res) { res.sendfile(__dirname + '/public/views/index.html'); }); //---------------helper function(s)-------------------// self.consoleprint = function(servervar){ var host = servervar.address().address; var port = servervar.address().port; console.log('example app listening @ http://%s:%s', host, port); }
and error:
c:\users\daniel\desktop\workspace\alarm_clock\index.js:17 var server = app.listen(8000, self.consoleprint(server)); ^ typeerror: undefined not function @ object.<anonymous> (c:\users\daniel\desktop\workspace\alarm_clock\index.js:17:36) @ module._compile (module.js:460:26) @ object.module._extensions..js (module.js:478:10) @ module.load (module.js:355:32) @ function.module._load (module.js:310:12) @ function.module.runmain (module.js:501:10) @ startup (node.js:129:16) @ node.js:814:3 12 may 01:01:36 - [nodemon] app crashed - waiting file changes before starting...
this resolve problem:
var server = app.listen(8000, function(){self.consoleprint(server)});
Comments
Post a Comment