NodeJS data throughput - node.js

I've set up a NodeJS server which can be accessed by a client. Every once in a while it's necessary to let the server connect to a second server and feed the information retrieved back to the client.
Connecting to the second server is the easy part, but to be honest I have no idea how to send it back to the client. res.write seems to be forbidden during the connection with the second server.
The connection from the client is handled by handleGetRequest. The connection with the second server starts at http.get.
var http = require('http');
var url = require('url');
var server = http.createServer(function(req, res) {
var url_parsed = url.parse(req.url, true);
if (req.method ==='GET') {
handleGetRequest(res, url_parsed);
} else {
res.end('Method not supported');
}
});
handleGetRequest = function(res, url_parsed) {
if (url_parsed.path == '/secondary') {
var OPTIONS = {
hostname: "localhost",
port: "8900",
path: "/from_primary"
}
http.get(OPTIONS, function(secget) {
resget.on('data', function(chunk) {
// either store 'chunk' for later use or send directly
});
}).on('error', function(e) {
console.log("Error " + e.message);
});
} else {
res.writeHead(404);
}
res.end('Closed');
};
server.listen(8000);
How do I send the chunk from http.request to the client?

I thinks passing the callback to the handleGetRequest will fix this issue:
if (req.method === 'GET') {
handleGetRequest(url_parsed, function (err, response) {
if (err) {
return res.sendStatus(500);
}
res.json(response);
});
} else {
res.end('Method not supported');
}
handleGetRequest = function (url_parsed, callback) {
// OPTIONS ...
http.get(OPTIONS, function(resget) {
var data = '';
resget.on('data', function(chunk) {
data += chunk;
});
resget.on('end', function() {
callback(null, data);
});
}).on('error', function(e) {
callback(e);
});
}

Thanks to #TalgatMedetbekov for the suggestions. I managed to implement it like this:
var http = require('http');
var url = require('url');
var server = http.createServer(function(req, res) {
var url_parsed = url.parse(req.url, true);
if (req.method ==='GET') {
handleGetRequest(res, url_parsed);
} else {
res.end('Method not supported');
}
});
handleGetSecondaryRequest = function(callback, res) {
var OPTIONS = {
hostname: "localhost",
port: "8900",
path: "/from_primary"
}
var data = null;
http.get(OPTIONS, function(func, data) {
func.on('data', function(chunk) {
data += chunk;
});
func.on('end', function() {
callback(res, data);
});
}).on('error', function(e) {
callback(res, e);
})
};
var secReqCallback = function(res, recData)
{
res.write(recData);
res.end("END");
};
handleGetRequest = function(res, url_parsed) {
if (url_parsed.path == '/secondary') {
handleGetSecondaryRequest(secReqCallback, res);
} else {
res.writeHead(404);
}
};
server.listen(8000);
It works, kind of. There's an 'undefined' in front of the string which I can't find the cause for, but the basic functionality works perfect.
The callback construction is necessary to synchronize the asynchronous nature of NodeJS.

Related

how to get the data from http.get

I have created a NodeJS server and created promises for the HTTP.get method and calling the get method function in created server but it showing error options.uri
var http = require('http');
var request = require('request');
var rese = null;
function initialize() {
var options = {
host: 'httpbin.org',
path: '/json',
};
return new Promise(function (resolve, reject) {
request.get(options, function (err, res, body) {
if (err) {
reject(err);
} else {
resolve(JOSN.parse(body));
}
/** res.on("data", function(chunk) {
//console.log("BODY: " + chunk);
result=chunk;
});*/
})
})
}
http.createServer(function (req, res) {
if (req.url == '/my') {
/**result=res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});*/
var initializePromise = initialize();
initializePromise.then(function (res) {
rese = result;
console.log("Initialized user details");
// Use user details from here
console.log(userDetails)
}, function (err) {
console.log(err);
});
//res.end(result);
}
else {
res.end('please find the correct path');
}
}).listen(2000);
error:options.uri is a required argument
Your get method should include uri, The request api get call structure is as follows,
request.get("http://bin.org/json", options, function (err, res, body) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(body));
}
/** res.on("data", function(chunk) {
//console.log("BODY: " + chunk);
result=chunk;
});*/
})
Try to make this changes, it will work

Use Firebase Function with error TypeError

I am very new to Node js, I just want to get the data from extenal xml from a website but I got an error from Firebase Function log TypeError: invalid media type. I think it come from when I try to do this task parseString(xml, function(err, result) { })
Anyone can help me, it will be great:
Here is my code on firebase function:
exports.getRate = functions.https.onRequest((req, res) => {
getRate = () => {
var url = "https://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx";
https.get(url, function(res) {
var xml = "";
res.on('error', function(error){
console.log(error, 'get data error');
})
res.on("data", function(chunk) {
xml += chunk;
console.log(xml, 'xml file');
});
res.on("end", function() {
var date = "";
let rateAUD = {
code: 'AUD/VND',
buy: 0,
sell: 0
};
let rateUSD = {
code: 'USD/VND',
buy: 0,
sell: 0
};
parseString(xml, function(err, result) {
console.log(xml, 'xml file');
date = result.ExrateList.DateTime[0];
if (result.ExrateList.Exrate[0].$.CurrencyCode == "AUD") {
rateAUD.buy = result.ExrateList.Exrate[0].$.Buy;
rateAUD.sell = result.ExrateList.Exrate[0].$.Sell;
} else {
console.log("They change the database list");
}
if (result.ExrateList.Exrate[18].$.CurrencyCode == "USD") {
rateUSD.buy = result.ExrateList.Exrate[18].$.Buy;
rateUSD.sell = result.ExrateList.Exrate[18].$.Sell;
} else {
console.log("They change the database list");
}
console.log(rateAUD, rateUSD, 'get data');
uploadDataToServer(date, { rateAUD, rateUSD });
if(err) {
console.log(err);
}
});
});
});
};
function uploadDataToServer(date, { rateAUD, rateUSD }) {
var db = admin.firestore();
let data = { rateAUD, rateUSD };
data.timeStamp = date;
console.log('upload success');
db.collection("liveRate").add(data),then((err)=> {
console.log(err);
});
}
return res.status(200)
.type('application / json')
.send('hello')
});
'
When I run the same code on another Nodejs playground, it works well.
Here is the link: https://repl.it/repls/MaroonSlateblueProfiler
So weird!
Ps: my payment option is ON.
The problem is that the client is sending the server what may or may not be a valid media type in an encoding the server cannot understand (as per the Content-Encoding header the client packaged with the request message).
Please try to set the content-type to xml:
getRate = () => {
var options = {
hostname: "www.vietcombank.com.vn",
port: 443,
path: "/ExchangeRates/ExrateXML.aspx",
headers: {
'Content-Type': 'application/xml'
}
};
https.get(options, function(res) {
...
});
}

Port redirection in node.js

I have a two server(running on two different port), one is for chat application, and another is for API generation server(user should register by providing company
details,and my algorithm gives a API key to the user).
The problem is, i am checking the valid API key,provided by the user, if API key is true then it should redirect to chat server(port no 5200).
But it doesn't work, please give any idea to resolve this issues.
Here is my code,
`
app.post('/checkAPIkey',function(req,res){
var apikey=req.query.apikey;
var apikey1=uuidAPIkey.isAPIKey(apikey);
if(apikey1){
res.writeHead(302, {
Location: 'http://localhost:5200'
});
}else{
res.end("error");
}
});`
What you need is called Request Forwarding.
Example:
const http = require('http');
app.post('/checkAPIkey', function(req,res){
var apikey=req.query.apikey;
var apikey1 = uuidAPIkey.isAPIKey(apikey);
if(apikey1){
const options = {
port: NEW_PORT,
hostname: 'NEW_HOST',
method: 'POST',
path: '/'
};
var reqForward = http.request(options, (newResponse) => {
//Do something with your newResponse
var responseData = "";
newResponse.on('data', (chunk) => {
//Add data response from newResponse
responseData += chunk;
});
newResponse.on('end', () => {
//Nothing more, send it with your original Response
response.send(responseData);
});
});
// If ERROR
reqForward.on('error', (e) => {
console.error('Error: ' + e);
});
// Write to the request
reqForward.write(YOUR_POST_DATA);
reqForward.end();
} else {
res.end("error");
}
});

Blocking requests coming from host A to host B nodejs

My nodejs httpserver (i'm not using express) is hosted in HOST A, domain: www.host-a.com and does this:
dispatcher.addListener("post", "/admin/insert_data", function(req, res) {
var body='';
req.on('data', function(chunk) {
body += chunk.toString();
});
req.on('end', function() {
var parsedbody = require('querystring').parse(body);
MongoClient.connect('mongodb://localhost:27017/database1', function(err, db) {
if (err) {
res.writeHead(500) ;
return res.end('Database offline') ;
}
console.log("Connected correctly to server");
var col = db.collection('mycollection');
col.insert(parsedbody, function() {
db.close();
var json = JSON.stringify({status: "0"});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(json);
});
});
});
});
The client side is the following:
$("form[name='manage_notizie']").submit(function(e) {
req="../admin/insert_data"
var tmp_notizia = $( "input[name=notizia]" ).val();
var tmp_id_notizia = $( "input[name=id_notizia]" ).val();
$.ajax({
url: req,
type: "POST",
data: {id_notizia:tmp_id_notizia, notizia:tmp_notizia},
async: false,
success: function (msg) {
location.reload();
},
error: function (msg) {
alert("Errore nel server")
},
cache: false,
});
e.preventDefault();
});
I know that by deafult, if I don't specify any access control allow origin, the server will respond only if the request arrives from itself (host a).
Now, for example if a request comes from www.host-b.com to www.host-a.com/insert_data, my server would not answer to the request (like I want) but it does the computing stuffs (which I don't want)
Am I missing something?

How can I know if the request has body?

I have this code I wrote for my server:
var http = require("http");
var url = require("url");
var routing = require("./RoutingPath");
var email = require('./NewServices.js');
var config = require ('./config.js');
function start() {
function onRequest(request, response) {
var path = url.parse(request.url).pathname;
var params = url.parse(request.url,true).query;
if (request.url === '/favicon.ico')
{
}
else
{
var data;
request.on('data', function(chunk) {
console.log("Received body data:");
console.log(chunk.toString());
data = chunk.toString();
});
routing.Route(path.toLowerCase(), params, data ,function(recordset){
response.writeHead(200, {"Content-Type": "application/json"});
if (recordset != null)
{
if (recordset.respondMessage == "GeneralSqlError")
{
var msg = JSON.stringify(recordset, null, 4);
console.log(msg);
email.SendEmailErrorNotify(msg, function(err){
if(err)
console.log(err);
});
}
else
{
console.log(JSON.stringify(recordset, null, 4));
}
response.write(JSON.stringify(recordset, null, 4));
}
response.end();
console.log();
});
}
}
http.createServer(onRequest).listen(8888);
console.log("Server has started!");
}
Sometimes the post request has data inbody. When there's a body I need to use it and process data.
The variable "data" in the following line should have data when it called:
routing.Route(path.toLowerCase(), params, data ,function(recordset){
...
How can I know if I have body?

Resources