Node.js leak in pipeline - node.js

I wrote a gateway for my microservices system.
Gateway was written in node.js
This gateway forwards the request to inside microservices, and responds client after getting result of their processing.
But memory consumed by this gateway continually increased, 24Mb increased every hour.
Could you help me to clarify why:
The following is code:
var express = require('express');
var requestify = require('requestify');
var app = express();
var request = require('request');
var config = require('./config.js');
var API_ERROR = 'error';
var DISCOVERY_NOTHING = 'nothing' ;
var TIMEOUT = 10000 ;//ms
function registerEntry(path,entryId) {
app.use(path, function(req, res) {
requestify.get(config.discovery+"?service_id="+entryId, {timeout:TIMEOUT}).then( function(response) {
var apiUrl = response.getBody() ;
if(apiUrl==DISCOVERY_NOTHING) {
res.send(API_ERROR);
}
var url = apiUrl + req.url ;
console.log(url);
req.pipe(request(url)).pipe(res).on('error', function(e) {
res.send(API_ERROR);
});
});
});
}
registerEntry('/authen','altp.authen') ;
registerEntry('/profile','altp.profile');
registerEntry('/question','altp.question');
registerEntry('/gameplay','altp.gameplay');
registerEntry('/admin','altp.admin');
app.listen(80, function() {
console.log('listening at 80');
});

Related

Make voice call using node js

I am using Both Java web server and node js(for chat)
Now I want to make voice call using Twilio`
I wrote code like this
var fs = require('fs');
var sslOptions = {};
var path = require('path');
var express = require('express');
var app = express();
var server = require('https').createServer(sslOptions, app);
const EventEmitter = require('events');
const myEE = new EventEmitter();
server.on("request", function (req, res) {
res.end("this is the response");
});
server.listen('8090', function(){
console.log("Secure Express server listening on port 8090");
});
var accountSid = 'AC***************************';
var authToken = "******************************";
var client = require('twilio')(accountSid, authToken);
var morgan = require('morgan');
var bodyParser = require('body-parser');
var twilio = require('twilio');
var VoiceResponse = twilio.twiml.VoiceResponse;
module.exports = server;
app.use(express.static(path.join(process.cwd(), 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use(morgan('combined'));
app.get('/', function(request, response) {
response.render('index');
});
app.post('/call', function(request, response) {
var salesNumber = request.body.salesNumber;
var url = 'http://' + request.headers.host + '/outbound/' + encodeURIComponent(salesNumber);
var options = {
to: '+91*******',
from: '+17******',
url: url,
};
client.calls.create(options)
.then((message) => {
response.send({
message: 'Thank you! We will be calling you shortly.',
});
})
.catch((error) => {
console.log('errot');
// console.log(error);
response.status(500).send('error');
});
});
app.post('/outbound/:salesNumber', function(request, response) {
var salesNumber = request.params.salesNumber;
var twimlResponse = new VoiceResponse();
twimlResponse.say('Thanks for contacting our sales department. Our ' +
'next available representative will take your call. ',
{ voice: 'alice' });
twimlResponse.dial(salesNumber);
response.send(twimlResponse.toString());
});
I am trying to make an ajax call from one of my javascript files to an express route
$.ajax({ url: '/call',
method: 'POST',
dataType: 'application/json',
processData: false,
data: {
phoneNumber: '+91*******',
salesNumber:'+17******** '
}
}).done(function(data) {
// The JSON sent back from the server will contain a success message
alert(data.message);
}).fail(function(error) {
//alert('errot');
alert(JSON.stringify(error));
});
when I execute this ajax call
it's looking for Java server and return 404 error
How can I solve this issue
Anyone, please help me to solve this issue

Defining the order of operations in an http.get() request - sync/async issue

I'm retrieving a web address as a URL parameter and then displaying whether the address returns a status code of 200 or not. The trouble is, it seems to only work after a couple seconds--the first time I run it, the code assigning 'mystatus' doesn't seem to be working properly. I feel like there is an issue with the order in which I am running things, but I'm not sure how to fix it.
var express = require("express");
var moment = require("moment");
var http = require("http");
var express = require("express");
var moment = require("moment");
var http = require("http");
var app=express();
var mystatus="";
app.get('/new/:name*', function(req,res){
//detect if name is a URL
//return output
http.get("http:"+req.params[0], function(thisres){
if (thisres.statusCode != 200){
mystatus = "Could not load";
}
else {
mystatus = "Loaded!";
}
}).on('error', function(e){
console.error(e);
})
res.json({url_part1: req.params.name,
url_part2: req.params[0],
status: mystatus
});
})
app.listen(8080, function(){
console.log("App listening on port 8080")
});
You're running your code node in the promise, so both http.get and res.json are running at the exact same time, so whichever one finishes first it doesn't matter.
Move the res.json response into the promise body of http.get so it will send the response when it gets it
var express = require("express");
var moment = require("moment");
var http = require("http");
var express = require("express");
var moment = require("moment");
var http = require("http");
var app=express();
var mystatus="";
app.get('/new/:name*', function(req,res){
//detect if name is a URL
//return output
http.get("http:"+req.params[0], function(thisres){
if (thisres.statusCode != 200){
mystatus = "Could not load";
}
else {
mystatus = "Loaded!";
}
res.json({url_part1: req.params.name,
url_part2: req.params[0],
status: mystatus
});
}).on('error', function(e){
console.error(e);
})
})
app.listen(8080, function(){
console.log("App listening on port 8080")
});

Start a proxy server with nodejs so that it serves requests preprended with "/wps_proxy/wps_proxy?url="

I want to start a proxy server with node.js so that it serves requests preprended with "/wps_proxy/wps_proxy?url=". I want it so I can use the wps-js library of north52 (check the installation tips) . I have already a server where I run my application.
What I did try until now is :
the server.js file
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var path = require("path");
var app = express();
app.use(express.static(__dirname + '/' + 'public'));
var urlencodedParser = bodyParser.urlencoded({ extended: false });
//****** this is my try ******************************
app.get('/wps_proxy/wps_proxy',function (req,res){
res.sendfile(__dirname + '/' + 'public/wps_proxy/wps-js/target/wps-js-0.1.2-SNAPSHOT/example.html');
if(req.query !== undefined){//because it enters sometimes without url
var http = require('http');
//Options to be used by request
var options = {
host:"geostatistics.demo.52north.org",//fixed given data
port:"80",
path:"/wps/WebProcessingService"
};
var callback = function(response){
var dat = "";
response.on("data",function(data){
dat+=data;
});
response.on("end", function(){
res.end(dat)
})
};
//Make the request
var req = http.request(options,callback);
req.end()
}
})
var ipaddress = process.env.OPENSHIFT_NODEJS_IP||'127.0.0.1';
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
app.set('port', port);
app.listen(app.get('port'),ipaddress, function() {
console.log( 'Server started on port ' + app.get('port'))
})
//***************************************
but its not working.. I think that the data are not sent back correctly..
This is a live example of what I want to do.. http://geoprocessing.demo.52north.org/wps-js-0.1.1/
and this is a live example of my application (check the console for errors) http://gws-hydris.rhcloud.com/wps_proxy/wps_proxy
I did find my answer from this post How to create a simple http proxy in node.js? so the way i solve it was:
app.get('/wps_proxy/wps_proxy',function (req,res){
var queryData = url.parse(req.url, true).query;
if (queryData.url) {
request({
url: queryData.url
}).on('error', function(e) {
res.end(e);
}).pipe(res);
}
else {
res.end("no url found");
}
})

Nodejs websocket communication with external system

I'm new to nodejs and I'm trying to solve communication issue with external system.
There is a gateway to external system which can handle websocket requests on port 5000. In the example below, when you request homepage, the nodejs opens websocket connection, then on websocket open event it sends request and waits for response which is used for the HTTP response.
Do you know how to open websocket to external system only once and handle requests based on request id?
var ws = require('ws');
var express = require('express');
var async = require('async');
var uuid = require('node-uuid');
app = express();
app.get('/', function (req, res) {
var webSocket = new ws('ws://localhost:5000/');
async.series([
function (callback) {
webSocket.on('open', function () {
webSocket.send(JSON.stringify({query:'data query', requestid: uuid.v4()}));
callback(null, 'data query');
});
},
function (callback) {
webSocket.on('message', function (data, flags) {
callback(null, data);
})
}
], function (err, results) {
res.setHeader('content-type', 'text/javascript');
res.send(results[1]);
webSocket.terminate();
});
});
var server = app.listen(3000, function () {
var port = server.address().port
console.log('Listening at %s', port)
});
Thanks for the hints. I ended with the following solution which does what I expect:
var ws = require('ws');
var express = require('express');
var uuid = require('node-uuid');
var requests = {};
app = express();
var webSocket = new ws('ws://localhost:5000/');
webSocket.on('open', function () {
console.log('Connected!');
});
webSocket.on('message', function (data, flags) {
var json = JSON.parse(data);
console.log(json.requestId);
var res = requests[json.requestId];
res.setHeader('content-type', 'text/javascript');
res.send(json.data);
delete requests[json.requestId];
});
app.get('/', function (req, res) {
var rid = uuid.v4();
requests[rid] = res;
webSocket.send(JSON.stringify({query:'data query', requestId: rid}));
});
var server = app.listen(3000, function () {
var port = server.address().port
console.log('Listening at %s', port)
});

Can't get Fibrous to work and sync my code

I'm using Node.js, Express.js, and node-serial port. Call to get the list of serial ports is asynchronous and I'm trying to make it synchronous with Fibrous, but can't get it to work. Here's the code:
var express = require('express');
var serialPort = require("serialport");
var fibrous = require('fibrous');
var app = express();
...
app.post('/', function(req, res, next) {
console.log('before execution');
detectDevices.sync();
console.log('after execution')
});
...
var detectDevices = fibrous(function() {
var ports = serialPort.sync.list();
console.log('mfg='+ports[0].manufacturer);
});
Error says "Can't wait without a fiber". I've tried to use 'Futures' from Fibrous and some other example, but can't make it work in the POST handler of Express.js. What is the proper way to have my detectDevices execute synchronously in the app.post() function?
Mike S is correct. The following code works very well:
var express = require('express');
var serialPort = require("serialport");
var fibrous = require('fibrous');
var app = express();
app.use(fibrous.middleware);
...
app.post('/', function(req, res, next) {
console.log('before execution');
detectDevices.sync();
console.log('after execution')
});
...
var detectDevices = fibrous(function() {
var ports = serialPort.sync.list();
for (var i = 0; i < ports.length; i++) {
console.log(port.comName);
console.log(port.pnpId);
console.log(port.manufacturer);
}
});

Resources