Я довольно новичок в программировании с node.js, и я не совсем уверен, почему я получаю эту ошибку. Функция выглядит правильно настроенной, и я не верю, что у меня есть какие-либо асинхронные проблемы b/c, которые должны учитываться с переменной self, которую я вставлял (я думаю). Я тоже попытался с этим, используя простой var consolePrint (...) В любом случае, это мой код ниже и журнал ошибок ниже.Вызов вспомогательной функции в node.js в обратном вызове?
/* 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 at http://%s:%s', host, port);
}
и ошибка:
C:\Users\Daniel\Desktop\workspace\alarm_clock\index.js:17
var server = app.listen(8000, self.consolePrint(server));
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\Users\Daniel\Desktop\workspace\alarm_clock\index.js:17:36)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
12 May 01:01:36 - [nodemon] app crashed - waiting for file changes before starting...
Почему бы вам не попробовать, как это .. 'вар сервер = app.listen (8000, функция() {self.consolePrint (сервер)});' –