routes in nodejs/express get called twice - javascript

im working in an existing project, and i have a problem driving me insane:
the routes are getting called twice or maybe even more, this happens when any route called take too long to respond
i think the problem is in this file, a dynamic routing attempt
var express = require('express');
var recaptcha = require('express-recaptcha');
//Site key, Secret key
recaptcha.init('xxxxx', 'yyyyy');
/**
* Routes middleware
* Check all requests
*/
module.exports = function(app, passport, memoryCache) {
var router = express.Router();
var config = require('app/config/settings.js');
router.use(function(req, res, next) {
console.log("touch");
console.log(req.url);
/**
* Make visible req / res on template layout
*/
res.locals.req = req;
res.locals.res = res;
/**
* Url filter
*/
var aryUrl = req.path.split('/').filter(Boolean);
try {
var root = '';
var controller = '';
var controllerPrototype = '';
var module = '';
var method = '';
var url = '';
var itemId = '';
var privacity = '';
var session = '';
switch (req.originalMethod) {
case 'GET':
method = 'get';
break;
case 'POST':
method = 'post';
break;
case 'PUT':
method = 'put';
break;
case 'DELETE':
method = 'delete';
break;
}
if (aryUrl.length == 0) {
root = '/';
} else {
root = aryUrl[0];
}
switch (root) {
case '/':
privacity = 'private';
session = 'General';
module = 'app/controllers/indexController';
controller = 'index';
controllerPrototype = 'root';
url = '/';
break;
case 'index':
privacity = 'private';
session = 'General';
module = 'app/controllers/indexController';
controller = 'index';
controllerPrototype = aryUrl[1];
url = '/index/' + aryUrl[1];
break;
case 'evaluaciones':
privacity = 'private';
session = 'PortalEval';
module = 'app/controllers/view-private/portalEvaluacionController';
controller = 'portalEvaluacion';
controllerPrototype = 'view';
url = '/evaluaciones';
if (aryUrl.length == 2) {
if (aryUrl[1] === 'antesdelcurso' || aryUrl[1] === 'despuesdelcurso') {
url = url + '/' + aryUrl[1];
} else {
controller = '';
}
}
break;
case 'relatores':
privacity = 'private';
session = 'RelatorEval';
module = 'app/controllers/portalRelatoresIndexController';
controller = 'portalRelatores';
controllerPrototype = 'view';
url = '/relatores';
break;
case 'api-pc':
privacity = 'public';
session = (req.user)? req.user.tipoSession : '';
var ctrl = aryUrl[1].replace(/\W+(.)/g, function(x, chr) {
return chr.toUpperCase();
});
ctrl = 'api' + ctrl.charAt(0).toUpperCase() + ctrl.slice(1);
module = 'app/controllers/api-public/' + ctrl + 'Controller';
controller = ctrl;
if (aryUrl.length == 4) {
itemId = '/:id';
}
controllerPrototype = aryUrl[2];
url = '/api-pc/' + aryUrl[1] + '/' + aryUrl[2] + itemId;
break;
case 'api-pt':
privacity = 'private';
session = (req.user)? req.user.tipoSession : '';
var ctrl = aryUrl[1].replace(/\W+(.)/g, function(x, chr) {
return chr.toUpperCase();
});
ctrl = 'api' + ctrl.charAt(0).toUpperCase() + ctrl.slice(1);
module = 'app/controllers/api-private/' + ctrl + 'Controller';
controller = ctrl;
if (aryUrl.length == 4) {
itemId = '/:id';
}
controllerPrototype = aryUrl[2];
url = '/api-pt/' + aryUrl[1] + '/' + aryUrl[2] + itemId;
break;
case 'view-pc':
privacity = 'public';
session = (req.user)? req.user.tipoSession : '';
var ctrl = aryUrl[1].replace(/\W+(.)/g, function(x, chr) {
return chr.toUpperCase();
});
module = 'app/controllers/view-public/' + ctrl + 'Controller';
controller = ctrl;
if (aryUrl.length == 4) {
itemId = '/:id';
}
controllerPrototype = aryUrl[2];
url = '/view-pc/' + aryUrl[1] + '/' + aryUrl[2] + itemId;
break;
case 'view-pt':
privacity = 'private';
session = (req.user)? req.user.tipoSession : '';
var ctrl = aryUrl[1].replace(/\W+(.)/g, function(x, chr) {
return chr.toUpperCase();
});
module = 'app/controllers/view-private/' + ctrl + 'Controller';
controller = ctrl;
if (aryUrl.length == 4) {
itemId = '/:id';
}
controllerPrototype = aryUrl[2];
url = '/view-pt/' + aryUrl[1] + '/' + aryUrl[2] + itemId;
break;
case 'auth':
/**
* Local Login
*/
var successGetLog = function(req, res) {
res.render('login/get.html', {
layout: 'layout/auth.html',
entorno: config.config_entorno.empresa,
message: req.flash('loginMessage'),
loginAttempts: req.flash('loginAttempts')
});
};
var successPostLog = function (req, res) {
var loginAttempts = res.locals.req.user.loginAttempts;
if (loginAttempts >= 4){
recaptcha.verify(req, function(error){
if(error){ //Falla de verificacion de captcha
res.render('login/get.html', {
layout: 'layout/auth.html',
entorno: config.config_entorno.empresa,
message: req.flash('loginMessage'),
loginAttempts: loginAttempts,
captcha:recaptcha.render()
});
} else { //Exito de verificacion de captcha
res.redirect('/');
}
});
} else {
res.redirect('/');
}
};
var successGetLogout = function(req, res) {
req.logout();
res.redirect('/');
};
router.route('/auth/login').get(successGetLog);
router.route('/auth/login').post(passport.authenticate("local-login", {failureRedirect: '/auth/logout'}), successPostLog);
router.route('/auth/logout').get(successGetLogout);
/**
* Encuesta Local Login
*/
var successGetPortalLog = function(req, res) {
if (req.query.rut != undefined || req.query.token == 'bciboost') {
if (req.query.rut != undefined && req.query.token == 'bciboost') {
console.log('casi');
req.body = {
rut: req.query.rut,
password: 'null'
};
passport.authenticate('portaleval-link-login', function(err, user, info) {
if (err) {
return next(err);
}
if (!user) {
res.render('portalevaluacionapp/portal_evaluacion_login.html', {
layout: 'layout/empty.html',
entorno: config.config_entorno.empresa,
message: req.flash('loginMessage'),
access: req.path,
});
}
req.logIn(user, function(err) {
if (err) {
return next(err);
}
if (user) {
res.redirect('/evaluaciones');
}
});
})(req, res, next);
} else {
res.render('portalevaluacionapp/portal_evaluacion_login.html', {
layout: 'layout/empty.html',
entorno: config.config_entorno.empresa,
message: 'Existe un problema al iniciar sesión',
access: req.path,
});
}
} else {
if (Object.keys(req.session.passport).length !== 0 && req.session.passport.constructor === Object && req.path === '/auth/evaluaciones' && req.session.passport.user.type === 'PortalEval') {
res.redirect('/evaluaciones');
} else {
res.render('portalevaluacionapp/portal_evaluacion_login.html', {
layout: 'layout/empty.html',
entorno: config.config_entorno.empresa,
message: req.flash('loginMessage'),
access: req.path,
});
};
}
}
var successPostPortalLog = function(req, res) {
res.redirect(req.path.replace('/auth', ''));
};
var successGetPortalLogout = function(req, res) {
req.logout();
res.redirect(req.path.replace('/logout', ''));
};
var successGetPortalRelatorLogout = function(req, res) {
req.logout();
res.redirect("/auth/relatores");
};
if (req.path === '/auth/evaluaciones' || req.path === '/auth/evaluaciones/antesdelcurso' || req.path === '/auth/evaluaciones/despuesdelcurso') {
var rutAux = req.body.rut;
req.body = {
rut: (typeof rutAux === 'undefined' || rutAux === '')? 'null': req.body.rut,
password: 'null'
};
}
router.route('/auth/evaluaciones').get(successGetPortalLog);
router.route('/auth/evaluaciones/antesdelcurso').get(successGetPortalLog);
router.route('/auth/evaluaciones/despuesdelcurso').get(successGetPortalLog);
router.route('/auth/evaluaciones').post(passport.authenticate("portaleval-login", {failureRedirect: '/auth/evaluaciones'}), successPostPortalLog);
router.route('/auth/evaluaciones/antesdelcurso').post(passport.authenticate("portaleval-login", {failureRedirect: '/auth/evaluaciones'}), successPostPortalLog);
router.route('/auth/evaluaciones/despuesdelcurso').post(passport.authenticate("portaleval-login", {failureRedirect: '/auth/evaluaciones'}), successPostPortalLog);
router.route('/auth/evaluaciones/logout').get(successGetPortalLogout);
router.route('/auth/evaluaciones/logout/antesdelcurso').get(successGetPortalLogout);
router.route('/auth/evaluaciones/logout/despuesdelcurso').get(successGetPortalLogout);
var successGetRelatorLog = function(req, res) {
res.render('portalrelatoresapp/portal_relatores_login.html', {
layout: 'layout/empty.html',
entorno: config.config_entorno.empresa,
message: req.flash('loginMessage'),
access: req.path,
});
};
var successPostRelatorPortalLog = function(req, res) {
res.redirect(req.path.replace('/auth', ''));
};
router.route('/auth/relatores').get(successGetRelatorLog);
router.route('/auth/relatores').post(passport.authenticate("portalrelatores-login", {failureRedirect: '/auth/relatores'}), successPostPortalLog);
router.route('/auth/relatores/logout').get(successGetPortalRelatorLogout);
break;
default:
break;
}
if (controller != '') {
var controllerDynamic = require(module);
controllerDynamic.req = req;
controllerDynamic.res = res;
/**
* Auth validation
*/
if (privacity == 'private') {
if (req.isAuthenticated()) {
if (session == 'General') {
if (controllerDynamic.auth.indexOf('PortalEval') != -1) {
var err = new Error(402);
throw err;
}
} else
if (session == 'PortalEval') {
if (controllerDynamic.auth.indexOf('General') != -1) {
var err = new Error(401);
throw err;
}
} else
if (session == 'RelatorEval') {
if (controllerDynamic.auth.indexOf('General') != -1) {
var err = new Error(401);
throw err;
}
}
//==============================================================================
else
if (session == 'PortalRelator') {
if (controllerDynamic.auth.indexOf('PortalRelator') == -1) {
var err = new Error(402);
throw err;
}
}
//==============================================================================
else {
var err = new Error(404);
throw err;
}
} else {
if (controllerDynamic.auth.indexOf('PortalEval') != -1) {
var err = new Error(402);
throw err;
} else
if (controllerDynamic.auth.indexOf('General') != -1) {
var err = new Error(401);
throw err;
} else {
var err = new Error(404);
throw err;
}
}
}
/**
* Check the "auth" atribute of a controller
*/
// if (controllerDynamic.auth.indexOf(controllerPrototype) != -1) {
// }
/**
* Final validation
*/
var isValid = true;
if (aryUrl.length > 4) {
isValid = false;
}
if (method == '') {
isValid = false;
}
if (typeof controllerDynamic[controllerPrototype] !== "function") {
isValid = false;
}
if (isValid) {
router.route(url)[method](function(ctrlReq, ctrlRes){
controllerDynamic[controllerPrototype](ctrlReq, ctrlRes);
});
} else {
res.status(404).send('Not found');
}
}
next();
} catch (e) {
if (e.message == 401) {
res.status(401).redirect('/auth/login');
} else
if (e.message == 402) {
res.status(401).redirect('/auth' + url);
} else {
res.status(404).send('Not found 2');
}
}
});
app.use('/', router);
};
in fact that console.log(touch) get called twice if the route doesn't respond fast (in less than 8 minutes) i had no idea what is going on
Thanks guys!

Related

Unhandled error Error: Data cannot be encoded in JSON error at firebase serverless functions

I'm trying to deploy an api for my application. Using these codes raises Unhandled error "Error: Data cannot be encoded in JSON.
const functions = require("firebase-functions");
const axios = require("axios");
exports.getDatas = functions.https.onCall(async (d)=>{
functions.logger.log(d["name"]);
cname = d["name"];
ts1=d["ts1"];
ts2=d["ts2"];
const data = await axios.get(
"https://api.coingecko.com/api/v3/coins/" +
cname +
"/market_chart/range?vs_currency=usd&from=" +
ts1 +
"&to=" +
ts2,
);
functions.logger.log(data);
return {data: data};
});
The error log is
Unhandled error Error: Data cannot be encoded in JSON: function httpAdapter(config) {
return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
var onCanceled;
function done() {
if (config.cancelToken) {
config.cancelToken.unsubscribe(onCanceled);
}
if (config.signal) {
config.signal.removeEventListener('abort', onCanceled);
}
}
var resolve = function resolve(value) {
done();
resolvePromise(value);
};
var rejected = false;
var reject = function reject(value) {
done();
rejected = true;
rejectPromise(value);
};
var data = config.data;
var headers = config.headers;
var headerNames = {};
Object.keys(headers).forEach(function storeLowerName(name) {
headerNames[name.toLowerCase()] = name;
});
// Set User-Agent (required by some servers)
// See https://github.com/axios/axios/issues/69
if ('user-agent' in headerNames) {
// User-Agent is specified; handle case where no UA header is desired
if (!headers[headerNames['user-agent']]) {
delete headers[headerNames['user-agent']];
}
// Otherwise, use specified value
} else {
// Only set header if it hasn't been set in config
headers['User-Agent'] = 'axios/' + VERSION;
}
if (data && !utils.isStream(data)) {
if (Buffer.isBuffer(data)) {
// Nothing to do...
} else if (utils.isArrayBuffer(data)) {
data = Buffer.from(new Uint8Array(data));
} else if (utils.isString(data)) {
data = Buffer.from(data, 'utf-8');
} else {
return reject(createError(
'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
config
));
}
if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
return reject(createError('Request body larger than maxBodyLength limit', config));
}
// Add Content-Length header if data exists
if (!headerNames['content-length']) {
headers['Content-Length'] = data.length;
}
}
// HTTP basic authentication
var auth = undefined;
if (config.auth) {
var username = config.auth.username || '';
var password = config.auth.password || '';
auth = username + ':' + password;
}
// Parse url
var fullPath = buildFullPath(config.baseURL, config.url);
var parsed = url.parse(fullPath);
var protocol = parsed.protocol || 'http:';
if (!auth && parsed.auth) {
var urlAuth = parsed.auth.split(':');
var urlUsername = urlAuth[0] || '';
var urlPassword = urlAuth[1] || '';
auth = urlUsername + ':' + urlPassword;
}
if (auth && headerNames.authorization) {
delete headers[headerNames.authorization];
}
var isHttpsRequest = isHttps.test(protocol);
var agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
var options = {
path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
method: config.method.toUpperCase(),
headers: headers,
agent: agent,
agents: { http: config.httpAgent, https: config.httpsAgent },
auth: auth
};
if (config.socketPath) {
options.socketPath = config.socketPath;
} else {
options.hostname = parsed.hostname;
options.port = parsed.port;
}
var proxy = config.proxy;
if (!proxy && proxy !== false) {
var proxyEnv = protocol.slice(0, -1) + '_proxy';
var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
if (proxyUrl) {
var parsedProxyUrl = url.parse(proxyUrl);
var noProxyEnv = process.env.no_proxy || process.env.NO_PROXY;
var shouldProxy = true;
if (noProxyEnv) {
var noProxy = noProxyEnv.split(',').map(function trim(s) {
return s.trim();
});
shouldProxy = !noProxy.some(function proxyMatch(proxyElement) {
if (!proxyElement) {
return false;
}
if (proxyElement === '*') {
return true;
}
if (proxyElement[0] === '.' &&
parsed.hostname.substr(parsed.hostname.length - proxyElement.length) === proxyElement) {
return true;
}
return parsed.hostname === proxyElement;
});
}
if (shouldProxy) {
proxy = {
host: parsedProxyUrl.hostname,
port: parsedProxyUrl.port,
protocol: parsedProxyUrl.protocol
};
if (parsedProxyUrl.auth) {
var proxyUrlAuth = parsedProxyUrl.auth.split(':');
proxy.auth = {
username: proxyUrlAuth[0],
password: proxyUrlAuth[1]
};
}
}
}
}
if (proxy) {
options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
setProxy(options, proxy, protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path);
}
var transport;
var isHttpsProxy = isHttpsRequest && (proxy ? isHttps.test(proxy.protocol) : true);
if (config.transport) {
transport = config.transport;
} else if (config.maxRedirects === 0) {
transport = isHttpsProxy ? https : http;
} else {
if (config.maxRedirects) {
options.maxRedirects = config.maxRedirects;
}
transport = isHttpsProxy ? httpsFollow : httpFollow;
}
if (config.maxBodyLength > -1) {
options.maxBodyLength = config.maxBodyLength;
}
if (config.insecureHTTPParser) {
options.insecureHTTPParser = config.insecureHTTPParser;
}
// Create the request
var req = transport.request(options, function handleResponse(res) {
if (req.aborted) return;
// uncompress the response body transparently if required
var stream = res;
// return the last request in case of redirects
var lastRequest = res.req || req;
// if no content, is HEAD request or decompress disabled we should not decompress
if (res.statusCode !== 204 && lastRequest.method !== 'HEAD' && config.decompress !== false) {
switch (res.headers['content-encoding']) {
/*eslint default-case:0*/
case 'gzip':
case 'compress':
case 'deflate':
// add the unzipper to the body stream processing pipeline
stream = stream.pipe(zlib.createUnzip());
// remove the content-encoding in order to not confuse downstream operations
delete res.headers['content-encoding'];
break;
}
}
var response = {
status: res.statusCode,
statusText: res.statusMessage,
headers: res.headers,
config: config,
request: lastRequest
};
if (config.responseType === 'stream') {
response.data = stream;
settle(resolve, reject, response);
} else {
var responseBuffer = [];
var totalResponseBytes = 0;
stream.on('data', function handleStreamData(chunk) {
responseBuffer.push(chunk);
totalResponseBytes += chunk.length;
// make sure the content length is not over the maxContentLength if specified
if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
// stream.destoy() emit aborted event before calling reject() on Node.js v16
rejected = true;
stream.destroy();
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
config, null, lastRequest));
}
});
stream.on('aborted', function handlerStreamAborted() {
if (rejected) {
return;
}
stream.destroy();
reject(createError('error request aborted', config, 'ERR_REQUEST_ABORTED', lastRequest));
});
stream.on('error', function handleStreamError(err) {
if (req.aborted) return;
reject(enhanceError(err, config, null, lastRequest));
});
stream.on('end', function handleStreamEnd() {
try {
var responseData = responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
if (config.responseType !== 'arraybuffer') {
responseData = responseData.toString(config.responseEncoding);
if (!config.responseEncoding || config.responseEncoding === 'utf8') {
responseData = utils.stripBOM(responseData);
}
}
response.data = responseData;
} catch (err) {
reject(enhanceError(err, config, err.code, response.request, response));
}
settle(resolve, reject, response);
});
}
});
// Handle errors
req.on('error', function handleRequestError(err) {
if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') return;
reject(enhanceError(err, config, null, req));
});
// set tcp keep alive to prevent drop connection by peer
req.on('socket', function handleRequestSocket(socket) {
// default interval of sending ack packet is 1 minute
socket.setKeepAlive(true, 1000 * 60);
});
// Handle request timeout
if (config.timeout) {
// This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
var timeout = parseInt(config.timeout, 10);
if (isNaN(timeout)) {
reject(createError(
'error trying to parse `config.timeout` to int',
config,
'ERR_PARSE_TIMEOUT',
req
));
return;
}
// Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
// And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
// At this time, if we have a large number of request, nodejs will hang up some socket on background. and the number will up and up.
// And then these socket which be hang up will devoring CPU little by little.
// ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
req.setTimeout(timeout, function handleRequestTimeout() {
req.abort();
var transitional = config.transitional || defaults.transitional;
reject(createError(
'timeout of ' + timeout + 'ms exceeded',
config,
transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
req
));
});
}
if (config.cancelToken || config.signal) {
// Handle cancellation
// eslint-disable-next-line func-names
onCanceled = function(cancel) {
if (req.aborted) return;
req.abort();
reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
};
config.cancelToken && config.cancelToken.subscribe(onCanceled);
if (config.signal) {
config.signal.aborted ? onCanceled() : config.signal.addEventListener('abort', onCanceled);
}
}
// Send the request
if (utils.isStream(data)) {
data.on('error', function handleStreamError(err) {
reject(enhanceError(err, config, null, req));
}).pipe(req);
} else {
req.end(data);
}
});
}
at encode (/workspace/node_modules/firebase-functions/lib/common/providers/https.js:162:11)
at encode (/workspace/node_modules/firebase-functions/lib/common/providers/https.js:156:22)
at encode (/workspace/node_modules/firebase-functions/lib/common/providers/https.js:156:22)
at encode (/workspace/node_modules/firebase-functions/lib/common/providers/https.js:156:22)
at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:334:22
at processTicksAndRejections (internal/process/task_queues.js:97:5)
First logger logs the parameter i gave correctly and the logger that logs data is in this format:
...["api.coingecko.com:443::::::::::::::::::"]},"keepAliveMsecs":1000,"maxFreeSockets":256,"scheduling":"fifo","keepAlive":false,"maxSockets":null},"_removedConnection":false,"writable":true},"status":200,"data":{"prices":[[1615345414698,37.27069164629981],[1615349310788,36.95627388647297],[1615352802175,37.48630338203377],[1615356202751,37.46442850999597],[1615360079361,37.642735963063906],[1615363905145,38.29435586902702],[1615367492353,38.313292928237594],[1615370461299,38.75503558097479],[1615374138056,38.24406575020552],[1615377815960,38.237026584388175],[1615381321332,38.93964664468625],[1615384813000,39.262646397955635],[1615388739874,39.15882057568881],[1615392094129,38.94488140309047],[1615395966875,38.79820936257378],[1615399312625,38.51637055616189],[1615403055037,38.59237008394828],[1615406529740,38.44087305010874],[1615410281814,37.71855645797291],[1615414278815,38.374824600586976],[1615417716420,38.4538669693684],[1615421045728,37.62772478442999],[1615425672990,36.8826465121472],[1615429587089,37.41958697414903],[1615432278494,37.34865694722488],[1615435254265,37.16289143388951],[1615439122292,37.14731463575248],[1615442523394,36.801517989796814],[1615446290102,37.02248224990424],[1615450361470,36.164787531097126],[1615453299572,36.46191265162147],[1615457172317,36.174755169666334],[1615460886498,37.05778010952229],[1615464298322,37.336909500902365],[1615469586325,37.56497212211488],[1615472126260,37.83046394206218],[1615474882979,37.252561357731096],[1615478498201,36.56190097084664],[1615482336185,36.83824760787625],[1615485957910,36.89351702770813],[1615489642151,37.589229946501746],[1615493390438,37.33184737771527],[1615496666244,37.29234576242379],[1615500577712,37.284260441548426],[1616866645601,1137195941.0307472],[1616870299925,1089416195.9864128],[1616873841648,1074341877.495249],[1616877368137,1061555457.3375872],[1616880970910,1077775411.1216433],[1616884693948,1064594490.6022671],[1616887998472,1087481667.611567],[1616891397951,1068140794.5197278],[1616894759953,1078753362.1719048],[1616898371565,1053546315.1245787],[1616902002474,1052498816.7223371],[1616905584364,1026915395.5541993],[1616909101481,1022271206.3215427],[1616912730390,997185793.1210617],[1616916434482,972130048.6316774],[1616919928611,988711196.2721183],[1616923534317,987299160.6191593],[1616926264719,975360472.6011684],[1616930074136,958327264.7346151],[1616933292776,935085970.8922312],[1616936940791,896217168.3654604],[1616940936847,878876312.6707534],[1616944090304,890504985.5476977],[1616948321869,896715385.5657766],[1616952007508,870767231.0865391],[1616955544207,880601758.4610194],[1616958381375,896794852.1077055],[1616962022167,929362788.5783823],[1616966479654,927502494.4691795],[1616969648773,880385481.5284289],[1616973545649,862329007.9935848],[1616977463095,840138544.6360805],[1616980359587,849727926.595521],[1616984356096,820616225.3306137],[1616987602367,898085663.0760688],[1616990444958,890215727.4112909],[1616995470635,914823340.6343507],[1616999032159,890922230.685704],[1617002651977,937214914.0703756],[1617005329558,976030203.3879734],[1617009370471,1061898884.4388478],[1617012348377,1111994349.2592206],[1617015705482,1175310227.1595278],[1617019895549,1217044915.3900926],[1617022941451,1204239369.9336267],[1617027118715,1225123359.1178432],[1617031210170,1191418570.9198012],[1617033728601,1257085051.9742537],[1617037882992,1261291734.3667347],[1617041858553,1265805909.4506621],[1617044547418,1261869965.5784621],[1617049418534,1225924891.220988],[1617052450394,1200646247.466799],[1617055896172,1209247034.0807025],[1617059684123,1249662106.3996315],[1617062561979,837849935.5380555],[1617066155823,1261094295.2039979],[1617070572708,1244044711.3556864],[1617074210159,1178503497.252399],[1617077106612,1184744920.254339],[1617080571662,1219164970.9205332],[1617084836477,1174744890.1399443],[1617087739776,1236332180.5454476],[1617092763739,1121685108.4046226],[1617096303391,1074005978.1362224],[1617100013739,1075898891.906641],[1617102136947,1041120230.0169744],[1617106411165,1021062028.7444541],[1617110588848,1004207600.6385714],[1617114148509,983098685.435342],[1617117449987,983878432.6976557],[1617120868725,943893192.0239582],[1617123806180,948379973.8680001],[1617128347360,948328240.0510467],[1617131244094,923477307.6495335],[1617134866719,918321070.6284192],[1617138697011,960178009.2986945],[1617142067857,974105207.7725881],[1617146083923,973959760.0729104],[1617149999086,959500047.5209063],[1617153094367,1007753562.6156206],[1617156698445,1021534121.1115336],[1617160175611,1028067427.0339341],[1617163928330,1007755251.8882328],[1617166924538,1023240773.0466446],[1617171886674,1037535813.1806505],[1617175133694,1101375379.7094195],[1617178435173,1136688478.90344],[1617182857658,1208366620.2561867],[1617185353773,1208823054.3509212],[1617188828477,1234197192.568771],[1617193393471,1707076315.380663],[1617196301983,1845668637.7358408],[1617199516026,1901877634.1385415],[1617203681947,2015292037.1305778],[1617207515426,2141098631.115179],[1617210224998,2343473154.2871637],[1617214323265,2329074198.4966955],[1617217968405,2461828129.1798186],[1617221653017,2493042958.539376],[1617224582971,2532015555.7692595],[1617228589364,2508661361.110037],[1617232204720,2590057969.924583],[1617235260464,2749780924.550207],[1617239367664,2791689438.967896],[1617243152558,2778422749.5901804],[1617246573894,2802892972.2612605],[1617250114952,2795446026.902383],[1617253276300,2837092221.188881],[1617257741390,2957061611.281718],[1617261111556,3025594776.954216],[1617264301698,3140730366.12618],[1617267704421,3230797741.627739],[1617272276500,3247001347.7404704],[1617275862720,3182990384.8873067],[1617279129292,2889317168.9977646],[1617283053665,2753527702.506779],[1617287046529,2700392654.8781624],[1617290204012,2616296684.424929],[1617293298853,2494255828.9768047],[1617296557242,2383424694.8900166],[1617301325511,2288268623.177356],[1617303766777,2297155897.636895],[1617307669347,2314935325.319679],[1617311721980,2259716784.056617],[1617314946823,2267889595.9127536],[1617319572007,2174169254.528509],[1617323182318,2097690604.8152165],[1617326033792,2110975746.1916978],[1617329489226,2126100629.800452],[1617332409284,2193182655.044224],[1617337211709,2199847063.5248647],[1617340611316,2167549077.601362],[1617344146863,2110348803.8388174],[1617347361962,2023115590.5637138],[1617351380142,1864316761.5098753],[1617354151186,1788973202.0040677],[1617359277447,1731207666.0376515],[1617361312976,1418566500.3106787],[1617366169158,1693859181.5518322],[1617369860769,1656689094.290342],[1617372306072,1660176536.7450612],[1617376754676,1722154482.4234965],[1617379285817,1915067128.493045],[1617383311995,1982773491.2907202],[1617387963188,1985155493.939231],[1617391564495,1827213471.6221747],[1617395202777,1932891922.7380657],[1617398214973,1937931474.560893],[1617401809690,1961473630.4188676],[1617405699909,1952347409.661483],[1617409553080,2172811188.054834],[1617412963837,2431917537.219363],[1617416445822,2666886575.1140027],[1617420431122,2769520722.4907126],[1617422613890,2797409323.779513],[1617427393260,2895546310.6951184],[1617431058021,2894169435.883223],[1617433696700,2651591430.614699],[1617437513773,3448548871.8910036],[1617441138039,3537764498.5278754],[1617444820385,3662623380.0181885],[1617448128419,3729999481.3895626],[1617452094944,3741683833.307362],[1617457034540,3761774670.321721],[1617460631688,3809173022.555833],[1617464335978,3711591162.8519845],[1617467879738,3759143118.4621553],[1617471447610,3693936894.7524076],[1617474960418,3833857114.2069917],[1617478639837,3888109113.59996],[1617482233320,3857034438.9984646],[1617485821346,3898924734.2645984],[1617489477282,3952661186.2182713],[1617493109729,4002501827.9437523],[1617495709286,3872814933.0218143],[1617499443431,3939579930.8108554],[1617503699037,3663106636.5813146],[1617507443725,3808705623.491391],[1617510706891,3786240536.055139],[1617512446242,3717882675.3539762],[1617516040645,3722966733.2957063],[1617519813304,3482249884.952562],[1617523351916,3345586253.508183],[1617526909722,3327000473.8244348],[1617530664916,3181835266.2617188],[1617534176048,3094776290.1306324],[1617537924632,3064167829.684326],[1617541493704,3112790145.252149],[1617545018360,2989449570.670528],[1617548594506,3016965749.017692],[1617552471191,2973530338.557288],[1617555933696,2759208177.1915674],[1617559387440,2662906186.1813793],[1617563034515,2521716547.9565806],[1617566483711,2454800946.788864],[1617570325792,2412175803.4922743],[1617573668989,2381142461.766321],[1617577282876,2228904400.2017546],[1617580896737,2203439508.717633],[1617584514686,2083961834.3200803],[1617588367701,1922511436.832222],[1617591869391,1816453643.1859522],[1617595346098,1783362433.1356776],[1617599069131,1767878927.408502],[1617602711113,1782121869.0062866],[1617606278078,1784322317.8294444],[1617609891135,1785304724.1970084],[1617613319383,1792007217.4012969],[1617617302304,1808002080.6732872],[1617620901014,1821923720.87615],[1617624265084,1769426364.6123836],[1617629555312,1731155926.337212],[1617631504259,1735378701.9021676],[1617635133537,1942437073.2385755],[1617638780500,1938122743.6976163],[1617642119732,1932182393.8447528],[1617645707597,1918416705.3436842],[1617649325384,1925855235.7182896],[1617653252063,1944708214.0244768],[1617656889033,1932665022.73478],[1617660329160,1943687775.1192245],[1617663683699,1971924479.2343264],[1617667435208,2101421530.2666874],[1617672769205,2175322213.812557],[1617674524812,2168578229.7784457],[1617678186353,2149217571.1759067],[1617681915267,2132725563.885806],[1617685469475,1907950838.2268875],[1617689189705,2026223167.4473426],[1617692670953,1991840998.8517568],[1617696101989,1958389716.0448081],[1617699877898,2027665770.2623076],[1617703590445,2045913908.1590445],[1617707076556,2057724347.183567],[1617710622851,1722203248.9530182],[1617714225215,2160140597.446546],[1617717905528,2192080372.5552874],[1617721488585,2199844279.449877],[1617724918808,2244159138.5689125],[1617728548093,2263548854.897557],[1617732187891,2106855536.9938018],[1617735969816,2268365061.664965],[1617739538518,1863113060.588111],[1617742875565,2296819840.9881096],[1617746516853,2308037223.56185],[1617750327052,2297405821.9954567],[1617754017835,2215648462.217197],[1617758617023,2112353884.9607923],[1617761085616,2094123582.0260437],[1617764518134,2101292245.7045105],[1617768287923,2104106865.0792534],[1617771810289,2127056476.4717],[1617775566730,2152196953.3590703],[1617778865860,2160666464.579131],[1617782881414,2201171213.1865735],[1617786249160,2203934869.139618],[1617789807394,2329117281.806726],[1617793383957,2333039138.899913],[1617796986959,2491205752.3653517],[1617800521125,2652604590.3673797],[1617804331429,2692817000.168284],[1617807822435,2121796914.212729],[1617811418506,2538097921.330415],[1617815037057,2572049083.87979],[1617818698211,2550478468.4248347],[1617822347031,2541491737.3311806],[1617825969097,2609118564.630648],[1617829326876,2651351577.1099257],[1617833305171,2429954572.560337],[1617837011298,2435043578.3313527],[1617840572965,2394428204.082167],[1617843841041,2446826032.07983],[1617848315742,2395082349.188743],[1617850339793,2376349751.741466],[1617852591890,2385498650.2366877],[1617855126472,2380054416.699361],[1617858732962,2424505564.216302],[1617862619633,2434391633.272485],[1617865876330,2410962812.9744062],[1617869233838,2516114320.406959],[1617872539799,2437748581.3302546],[1617876293610,2247205079.171164],[1617880005259,2149347865.150653],[1617883394235,1893777066.5168178],[1617886836203,1757412804.559377],[1617892197847,1668727963.8671286],[1617894162445,1631584545.4824028],[1617897737215,1596293896.725426],[1617901282046,1525523967.3370435],[1617905003853,1370316987.26801],[1617908631874,1358993841.079183],[1617912335250,1404691449.9164236],[1617915995319,1379405950.1047523],[1617919567600,1366246502.7408085],[1617923270275,1289254721.1461022],[1617926622919,1386402238.6371279],[1617930228859,1384851642.1789908],[1617933705891,1365548610.2907162],[1617937372258,1357266138.9978309],[1617941122560,1335764096.6047564],[1617944870896,1322495289.1105938],[1617948462328,1283751933.8339043],[1617951863802,1272489837.990008],[1617955666499,1259096045.8789752],[1617958890026,1247182948.0102005],[1617962609987,1220448763.9536679],[1617966256703,1222538618.147044],[1617969964555,1148194206.4734476],[1617973333279,1199996169.7479842],[1617977646106,1154935691.529977],[1617980504476,1144564005.003322],[1617984273306,1132822242.6037295],[1617987925282,1136733019.0246003],[1617991396077,1139090847.1565342],[1617994822351,1133169530.4839995],[1617998615234,1113274570.5832539],[1618002141094,1094805189.6349592],[1618005876460,1035579604.067034],[1618009282025,1090335224.3969038],[1618013035782,1063984405.5106469],[1618016519119,1058097513.8615906],[1618020114108,1065381128.0365001]]}}
When this code invoked it logs data correctly but i can not return it at last. Anyone can help?
The problem appears to be that you're trying to return the entire Axios response. This cannot be serialised as JSON due to circular references.
Simply return the response data instead. You can also make your URL construction simpler (and safer) using the params option
exports.getDatas = functions.https.onCall(async ({ name, ts1, ts2 }) => {
functions.logger.log(name);
// 👇 note the destructure here
const { data } = await axios.get(
`https://api.coingecko.com/api/v3/coins/${encodeURIComponent(name)}/market_chart/range`,
{
params: {
vs_currency: "usd",
from: ts1,
to: ts2,
}
}
);
functions.logger.log(data);
return { data };
});

Is there a way to wait for the calculations before moving to next iteration in Javascript

In the following function before the calculation for productId and reqQuantity finished corresponding to the else if condition. The commands after it are ran.
addBasketItem.quantityCheck = () => (req, res, next) => {
if (req.method === 'POST' || req.method === 'PUT') {
// console.log(extractParam(req))
var result = utils.parseJsonCustom(req.rawBody)
var productId = 0
var reqQuantity = 0
if( req.method === 'POST') {
var productIds = []
var basketIds = []
var quantities = []
for (var i = 0; i < result.length; i++) {
if (result[i].key === 'ProductId') {
productIds.push(result[i].value)
} else if (result[i].key === 'BasketId') {
basketIds.push(result[i].value)
} else if (result[i].key === 'quantity') {
quantities.push(result[i].value)
}
}
productId = productIds[0]
console.log("productIdInstantiated:", productId)
reqQuantity = quantities[0]
} else if (req.method === 'PUT') {
var pID = req.url
models.BasketItem.findAll({ where: { id: pID.replace('/', '') } }).then((item) => {
productId = item[0].dataValues.ProductId <---- Here
console.log("productIdInside:", productId)
reqQuantity = result[0].value <---- Here
})
}
console.log("productIdQueried:", productId)
models.Product.findAll({ where: { id: productId } }).then((product) => {
const availableQuantity = product[0].dataValues.quantity
if (availableQuantity < reqQuantity) {
res.status(401).send('{\'error\' : \'Quantity Unavailable\'}')
} else {
next()
}
}).catch(error => {
next(error)
})
} else {
next()
}
}
You can use async/await for this:
addBasketItem.quantityCheck = async (req, res, next) => {
if (req.method === 'POST' || req.method === 'PUT') {
// console.log(extractParam(req))
var result = await utils.parseJsonCustom(req.rawBody)
var productId = 0
var reqQuantity = 0
....
You can change the order and store the promise in a variable.
After that you can just call .then() on the variable.
addBasketItem.quantityCheck = () => (req, res, next) => {
var promise;
//Some stuff going on here..
promise = models.BasketItem.findAll({ where: { id: pID.replace('/', '') } });
//After "else if (req.method === 'PUT')" (Or somewhere else..)
promise.then(item => {
productId = item[0].dataValues.ProductId;
console.log("productIdInside:", productId);
reqQuantity = result[0].value;
models.Product.findAll({ where: { id: productId } }).then((product) => {
const availableQuantity = product[0].dataValues.quantity
if (availableQuantity < reqQuantity) {
res.status(401).send('{\'error\' : \'Quantity Unavailable\'}')
} else {
next()
}
}).catch(error => {
next(error)
});
});
}

Not able to hook up listener on eventEmitter

I'm trying to setup email notification using node's events module. I'm not able to make it work, as in its not doing when email event is emitted. Not sure whats wrong. Could you please help me debug this code. Thanks in advance!
email.js
var express = require('express');
var nodemailer = require('nodemailer');
var events = require('events');
var util = require('util');
var _ = require('lodash');
var Notify = function() {
events.EventEmitter.call(this);
var transporter = nodemailer.createTransport();
this.eventNames = {
newBug: 'new bug',
updatedBug: 'updated bug',
commentedOnBug: 'commented on bug',
updatedAndCommentedOnBug: 'updated and commented on bug',
};
this.notifier = function(eventName, data) {
this.on('email', function() {
console.log('email notifier......');
})
};
this.notify = function(eventName, data) {
console.log('notify...');
this.emit('email', data);
};
this.send = function(eventName, data) {
var email = {
from: 'new_bugtrack#example.com',
to: '',
subject: '',
text: ''
};
email.to = data.assignTo.email || '';
switch (eventName) {
case this.eventNames.newBug:
email.subject = 'Created new bug ' + data.id
email.text = data.title
break;
case this.eventNames.updatedBug:
email.subject = _.last(data.changeHistory).updatedBy.name + ' updated bug-' + data.id
email.text = data._.last(data.changeHistory).change
break;
case this.eventNames.commentedOnBug:
email.subject = _.last(data.changeHistory).updatedBy.name + ' commented on bug-' + data.id
email.text = data._.last(data.changeHistory).comment
break;
case this.eventNames.updatedAndCommentedOnBug:
email.subject = _.last(data.changeHistory).updatedBy.name + 'updated and commented on bug-' + data.id
email.text = _.last(data.changeHistory).change + '-----' + _.last(data.changeHistory).comment
break;
default:
break;
}
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {    
email.to = 'example#example.com';
}
console.log('email emit activated');
transporter.sendMail(email);
};
};
util.inherits(Notify, events.EventEmitter);
// dont't send emails in dev/test env
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {    
Notify.send = function(eventName, data) {
console.log('emailed data', JSON.stringify(data, null, 2));
};
}
module.exports.Notify = Notify;
bug.controller.js
var marklogic = require('marklogic');
var conn = require('../../config/db-config.js').connection;
var db = marklogic.createDatabaseClient(conn);
var Notify = require('../../email/email').Notify;
var email = new Notify();
console.log('notify', email);
// just a simple function for demo
exports.count = function(req, res) {
db.documents.query(
q.where(
q.collection('bugs')
)
.slice(1, 1)
.withOptions({
debug: true,
categories: 'metadata'
})
).result(function(response) {
email.notify(email.eventNames.newBug, {a: 2})
res.status(200).json({
count: response[0].total
});
});
};
...
...
...
I see the log statement notify being printed from notify() function but not email notifier which is hooked to email event
Indeed, the function below is never invoked, so you are not attaching any listener to the event emitter.
this.notifier = function(eventName, data) {
this.on('email', function() {
console.log('email notifier......');
})
};
Change it as follows:
var notifier = function(data) {
console.log('email notifier......');
};
this.on('email', notifier);

deploy zensh/jsgen to appfog

for appfog I usually use this code:
var host = process.env.VCAP_APP_HOST || "127.0.0.1";
var port = process.env.VCAP_APP_PORT || 1337;
http.createServer(function (req, res) {
// ...
}).listen(port, host);
Now I found a very interesting forum software . Here is the app.js:
'use strict';
/*global global, require, process, module, jsGen, _restConfig*/
var fs = require('fs'),
path = require('path'),
zlib = require('zlib'),
util = require('util'),
http = require('http'),
domain = require('domain'),
serverDm = domain.create(),
processPath = path.dirname(process.argv[1]);
global.jsGen = {}; // 注册全局变量jsGen
jsGen.version = '0.6.6';
serverDm.on('error', function (err) {
delete err.domain;
jsGen.serverlog.error(err);
});
serverDm.run(function () {
jsGen.conf = module.exports.conf = require('./config/config'); // 注册rrestjs配置文件
jsGen.module = {};
jsGen.module.os = require('os');
jsGen.module.xss = require('xss');
jsGen.module.then = require('thenjs');
jsGen.module.marked = require('marked');
jsGen.module.rrestjs = require('rrestjs');
jsGen.module.mongoskin = require('mongoskin');
jsGen.module.nodemailer = require('nodemailer');
jsGen.serverlog = jsGen.module.rrestjs.restlog;
jsGen.lib = {};
jsGen.lib.msg = require('./lib/msg.js');
jsGen.lib.json = require('./lib/json.js');
jsGen.lib.tools = require('./lib/tools.js');
jsGen.lib.email = require('./lib/email.js');
jsGen.lib.redis = require('./lib/redis.js');
jsGen.lib.CacheLRU = require('./lib/cacheLRU.js');
jsGen.lib.converter = require('./lib/anyBaseConverter.js');
jsGen.Err = jsGen.lib.tools.Err;
jsGen.dao = {};
jsGen.dao.db = require('./dao/mongoDao.js').db;
jsGen.dao.tag = require('./dao/tagDao.js');
jsGen.dao.user = require('./dao/userDao.js');
jsGen.dao.index = require('./dao/indexDao.js');
jsGen.dao.article = require('./dao/articleDao.js');
jsGen.dao.message = require('./dao/messageDao.js');
jsGen.dao.collection = require('./dao/collectionDao.js');
jsGen.thenErrLog = function (defer, err) {
jsGen.serverlog.error(err);
};
var redis = jsGen.lib.redis,
then = jsGen.module.then,
each = jsGen.lib.tools.each,
CacheLRU = jsGen.lib.CacheLRU,
extend = jsGen.lib.tools.extend,
resJson = jsGen.lib.tools.resJson,
TimeLimitCache = jsGen.lib.redis.TimeLimitCache;
then(function (defer) {
redis.initConfig(jsGen.lib.json.GlobalConfig, defer); // 初始化config缓存
}).then(function (defer, config) {
jsGen.config = config;
if (!jsGen.config.date) { // config缓存未赋值,则从MongoDB取值
then(function (defer2) {
jsGen.dao.index.getGlobalConfig(defer2);
}).then(function (defer2, config) {
defer2(null, config);
}, function (defer2, err) {
// MongoDB无值,初始化数据库
require('./api/install.js')().then(function () {
defer2(null, jsGen.lib.json.GlobalConfig);
}).fail(defer2);
}).then(function (defer2, config) {
each(jsGen.config, function (value, key, list) {
if (key in config) {
list[key] = config[key]; // 写入config缓存
}
});
defer(null, jsGen.config);
}).fail(defer);
} else {
defer(null, config);
}
}).then(function (defer, config) {
var api = ['index', 'user', 'article', 'tag', 'collection', 'message', 'rebuild'];
jsGen.cache = {};
jsGen.cache.tag = new CacheLRU(config.tagCache);
jsGen.cache.user = new CacheLRU(config.userCache);
jsGen.cache.list = new CacheLRU(config.listCache);
jsGen.cache.article = new CacheLRU(config.articleCache);
jsGen.cache.comment = new CacheLRU(config.commentCache);
jsGen.cache.message = new CacheLRU(config.messageCache);
jsGen.cache.collection = new CacheLRU(config.collectionCache);
jsGen.cache.timeInterval = new TimeLimitCache(config.TimeInterval, 'string', 'interval', false);
jsGen.cache.pagination = new TimeLimitCache(config.paginationCache, 'array', 'pagination', true);
jsGen.robotReg = new RegExp(config.robots || 'Baiduspider|Googlebot|BingBot|Slurp!', 'i');
jsGen.api = {};
each(api, function (x) {
jsGen.api[x] = {}; // 初始化api引用,从而各api内部可提前获取其它api引用
});
each(api, function (x) {
extend(jsGen.api[x], require('./api/' + x + '.js')); // 扩展各api的具体方法
});
fs.readFile(processPath + '/package.json', 'utf8', defer); // 读取软件信息
}).then(function (defer, data) {
data = JSON.parse(data);
data.version = jsGen.version;
data.nodejs = process.versions.node;
data.rrestjs = _restConfig._version;
jsGen.config.info = data;
redis.userCache.index.total(defer); // 读取user缓存
}).then(function (defer, users) {
var rebuild = jsGen.api.rebuild;
if (!users) { // user缓存为空,则判断redis缓存为空,需要初始化
// 初始化redis缓存
then(function (defer2) {
rebuild.user().all(defer2);
}).then(function (defer2) {
rebuild.tag().all(defer2);
}).then(function (defer2) {
rebuild.article().all(defer);
}).fail(defer);
} else {
defer();
}
}).then(function (defer) {
http.createServer(function (req, res) {
var dm = domain.create();
function errHandler(err, res, dm) {
delete err.domain;
try {
res.on('finish', function () {
//jsGen.dao.db.close();
process.nextTick(function () {
dm.dispose();
});
});
if (err.hasOwnProperty('name')) {
res.sendjson(resJson(err));
} else {
jsGen.serverlog.error(err);
res.sendjson(resJson(jsGen.Err(jsGen.lib.msg.MAIN.requestDataErr)));
}
} catch (error) {
delete error.domain;
jsGen.serverlog.error(error);
dm.dispose();
}
}
function router(req, res) {
if (req.path[0] === 'api' && jsGen.api[req.path[1]]) {
jsGen.api[req.path[1]][req.method.toUpperCase()](req, res); // 处理api请求
} else if (req.path[0].toLowerCase() === 'sitemap.xml') {
jsGen.api.article.sitemap(req, res); // 响应搜索引擎sitemap,动态生成
} else if (req.path[0].slice(-3).toLowerCase() === 'txt') {
// 直接响应static目录的txt文件,如robots.txt
then(function (defer) {
fs.readFile(processPath + '/static/' + req.path[0], 'utf8', defer);
}).then(function (defer, txt) {
res.setHeader('Content-Type', 'text/plain');
res.send(txt);
}).fail(res.throwError);
} else if (jsGen.robotReg.test(req.useragent)) {
jsGen.api.article.robot(req, res); // 处理搜索引擎请求
} else {
jsGen.config.visitors = 1; // 访问次数+1
res.setHeader('Content-Type', 'text/html');
if (jsGen.cache.indexTpl) {
res.send(jsGen.cache.indexTpl); // 响应首页index.html
} else {
then(function (defer) {
fs.readFile(processPath + '/static/index.html', 'utf8', defer);
}).then(function (defer, tpl) {
jsGen.cache.indexTpl = tpl.replace(/_jsGenVersion_/g, jsGen.version);
res.send(jsGen.cache.indexTpl);
}).fail(res.throwError);
}
}
}
res.throwError = function (defer, err) { // 处理then.js捕捉的错误
if (!util.isError(err)) {
err = jsGen.Err(err);
}
errHandler(err, res, dm);
};
dm.on('error', function (err) { // 处理domain捕捉的错误
errHandler(err, res, dm);
});
dm.run(function () {
router(req, res); // 运行
});
}).listen(jsGen.module.rrestjs.config.listenPort);
console.log('jsGen start!');
}).fail(function (defer, err) {
throw err;
});
});
Do you know how to modify this code to deploy to appfog?
appfog and cloudfoundry support auto-reconfiguration for node.js applications. So the code should run on AppFog without any modifications.
However if you want to use those environment variables, you can start by modifying the line .listen(jsGen.module.rrestjs.config.listenPort); so it reads:
.listen(process.env.VCAP_APP_PORT, process.env.VCAP_APP_HOST);

function as parameter in node.js module

in this code: if i try to pass this.handler as parameter to server.createServer() , then i get no response (the page keeps loading in browser). but if i use server.createServer(function(req, res) { //same code here as in handler() }) , then it works. what am i doing wrong?
var Con = module.exports = function() {
process.EventEmitter.call(this);
}
var createServer = module.exports.createServer = function(options) {
console.log('start');
this.port = options.port || 9122;
this.secure = options.secure || false;
if(this.secure === true)
if(!options.key || !options.certificate)
this.secure = false;
else {
this.key = options.key;
this.certificate = options.certificate;
}
if(this.secure) {
this.server = require('https');
var fs = require('fs');
var opt = {
key: fs.readFileSync('privatekey.pem'),
cert: fs.readFileSync('certificate.pem')
};
this.server.createServer(opt, this.handler).listen(this.port);
} else {
this.server = require('http');
this.server.createServer(this.handler).listen(this.port);
}
}
Con.prototype.handler = function(req, res) {
console.log('request');
res.writeHead(200);
res.write(req.url);
res.end();
}
var Con = function() {
process.EventEmitter.call(this);
}
That's your constuctor
module.exports = new Con();
That's your instance
var createServer = module.exports.createServer = function(options) {
console.log('start');
this.port = options.port || 9122;
this.secure = options.secure || false;
if(this.secure === true)
if(!options.key || !options.certificate)
this.secure = false;
else {
this.key = options.key;
this.certificate = options.certificate;
}
if(this.secure) {
this.server = require('https');
var fs = require('fs');
var opt = {
key: fs.readFileSync('privatekey.pem'),
cert: fs.readFileSync('certificate.pem')
};
this.server.createServer(opt, this.handler).listen(this.port);
} else {
this.server = require('http');
this.server.createServer(this.handler).listen(this.port);
}
}
.createServer is now a method on the instance rather then the constructor.
Since it's on the instance it also has access to the .handler method defined on the instance through the prototype.

Categories

Resources