Working on a bot in Node.js which expands on the data created by another bot. That Bot outputs all it's data to a JSON page https://mee6.xyz/levels/267482689529970698?json=1
But I can't see the console output of data produced by JSONStream.
How can I get it so i can use it for my extension system?
var request = require('request')
, JSONStream = require('JSONStream')
, es = require('event-stream')
request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'})
.pipe(JSONStream.parse('rows.*'))
.pipe(es.mapSync(function (data) {
console.error(data)
var stream = JSONStream.parse(['rows', true, 'doc']) //rows, ANYTHING, doc
stream.on('data', function(data) {
console.log('received:', data);
});
//emits anything from _before_ the first match
stream.on('header', function (data) {
console.log('header:', data) // => {"total_rows":129,"offset":0}
})
}))
There are a couple of problems.
It seems that you have mixed 2 approaches described in JSONStream documentation.
First, the JSON you're requesting simply doesn't contain any fields with the name 'row' that is why this doesn't work: .pipe(JSONStream.parse('rows.*'))
To see the output you can do something like this:
request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'})
//So I'm getting all the players records
.pipe(JSONStream.parse('players.*'))
.pipe(es.mapSync(function (data) {
console.log(data);
}));
Checkout JSONStream and JSONPath docs.
The second is that this stream stream = JSONStream.parse(['rows', true, 'doc']) is created and then simply lost. You're note using it.
So if you don't like the 1st way you can do:
var stream = JSONStream.parse(['players']);
stream.on('data', function(data) {
console.log('received:', data);
});
stream.on('header', function (data) {
console.log('header:', data);
});
//Pipe your request into created json stream
request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'})
.pipe(stream);
Hope this helps.
Related
In my Node JS server I have this route handler that sends a request to a third party API to get a username:
app.get('/players/:player', apiLimiter, function(request, response) {
const player = request.params.player;
const api_url = `https://api.com/shards/steam/players?filter[playerNames]=${player}`;
var options = {
method: "GET",
observe: 'body',
};
let apiRequest = https.request(api_url, options, function (res) {
let data = "";
res.on("data", chunk => {
data += chunk;
})
res.on("end", () => {
let objectParsed = JSON.parse(JSON.stringify(data));
response.send(objectParsed);
})
if(!player) {
res.status(404).send("Not found.");
}
})
apiRequest.end();
})
This works fine to get a user that exists. However, if I put in a fake username to my /players page, that page still loads with a 200 status instead of getting a 404 response. The page loads and looks broken because it's not actually getting any data from the API.
I feel like this is a dumb question .. In my research I have found how to handle errors if it's just the route, and not if it's the route dependent on the path parameter as in /players/:player
I found a question that was similar to mine (How to throw a 404 error in express.js?) and I tried using an If statement: if (!player){res.status(404).send("Not found."); } but no dice. Am I using this if statement in the wrong place?
How can I get my Node JS server to respond with a 404 if the user from the database doesn't exist?
You have to check the result of the API call and see if you got valid data back and send the 404 there. I also added a check to make sure something was passed for the player name and send back a 400 (bad request) if there's no player specified at all:
app.get('/players/:player', apiLimiter, function(request, response) {
const player = request.params.player;
if (!player) {
res.status(400).send("No player specified.");
return;
}
const api_url = `https://api.com/shards/steam/players?filter[playerNames]=${player}`;
var options = {
method: "GET",
observe: 'body',
};
let apiRequest = https.request(api_url, options, function(res) {
let data = "";
res.on("data", chunk => {
data += chunk;
})
res.on("end", () => {
let objectParsed = JSON.parse(data);
// test objectParsed here
if (!some condition in objectParsed) {
res.status(404).send("No data for that player name.");
} else {
response.send(objectParsed);
}
});
});
apiRequest.end();
});
Also, you don't want JSON.parse(JSON.stringify(data)) here. Your data is already a string. Just do JSON.parse(data).
FYI, if you use a small http request library such as got(), this code gets a lot simpler as it accumulates the response and parses the JSON for you in one line of code as in:
let data = await got(options).json()
I increase a value at the server by running an Ajax call and want to update my UI after doing this
function increaseHitpoints(){
$.ajax({
type: 'GET',
url: 'http://localhost:8888/incHp/2312'
}).done(function (data) {
$("#txtHitpoints").html(data);
});
}
In my app.js I read a JSON file, manipulate the value, write it back to the file and return it to the client
app.get('/incHp/:id', function (req, res) {
var database = './database.json';
fs.readFile(database, 'utf8', function (err, data) { // read the data
var json = JSON.parse(data);
var users = json.users;
var hitpoints;
users.find(u => {
if (u.id === Number(req.params.id)) { // get the user by id
u.hitpoints++;
hitpoints = u.hitpoints;
}
});
json = JSON.stringify(json);
fs.writeFile(database, json, (err) => { // update the JSON file
// -> missing part here <-
});
});
});
what do I have to pass into the missing part if I want to return the new value? The new value would be hitpoints
I tried res.send(hitpoints); but it seems this function wants to return a status code, not a value.
If you send a numerical value, it will be observed as an HTTP response code
https://expressjs.com/en/api.html#res
But you can send your hitpoints as a string res.send(hitpoints.toString())or as json res.send({hits: hitpoints});
Depends on what format you want your response to be. I prefer using JSON. So in JSON case you would do this:
fs.writeFile(database, json, (err) => {
res.status(200).json({yourKey: yourValue});
});
Then you can access the JSON object in your frontend:
$("#txtHitpoints").html(data.yourKey);
In the case, the actually conversation-simple have one function with all the values, but the function update every time if flows conversation.
I want create one function or other form to be able to capture all that data that is currently on the data.
In the case have Intents, context, entities, etc.
conversation.message(payload, function(err, data) {
if (err) {
return res.status(err.code || 500).json(err);
}
return res.json(updateMessage(payload, data));
});
});
The data inside updateMessage parameter have all I need, but if I create other function and try get this values, does not work.
In the case I use the values and get with app.js for open some REST webservice.
I try it:
function login (req, res) {
numberOrigin = null;
sessionid = null;
var dataLogin = {
data: { "userName":"xxxxx","password":"xxxxx","platform":"MyPlatform" },
headers: { "Content-Type": "application/json" }
};
client.registerMethod("postMethod", "xxxxxxxxxxxxxxx/services/login", "POST");
client.methods.postMethod(dataLogin, function (data, response) {
if(Buffer.isBuffer(data)){
data = data.toString('utf8');
console.log(data);
var re = /(sessionID: )([^,}]*)/g;
var match = re.exec(data);
var sessionid = match[2]
console.log(sessionid);
}
});
}
function openRequest(data, sessionid, numberOrigin ){
//console.log(data); dont show the values.. show the data response of login
var dataRequest = {
data: {"sessionID": sessionid,
"synchronize":false,
"sourceRequest":{
"numberOrigin":numberOrigin,
"description": JSON.stringify(data.context.email) } },
headers: { "Content-Type": "application/json" }
};
numberOrigin +=1;
client.post("xxxxxxxxxxxxxxxxxx/services/request/create", dataRequest, function (data, response) {
if(Buffer.isBuffer(data)){
data = data.toString('utf8');
console.log(data);
}
});
}
function updateMessage(res, input, data, numberOrigin) {
var email = data.context.email; // this recognize but this function is responsible for other thing
if (email === 'xxxxxxxxxxxx#test.com') {
console.log(data);
login(data);
openRequest(data, sessionid, numberOrigin)
}
}
In case, I just want get the values with my app.js for use inside REST. I got it with ajax but everything on the client side (index.html), and that made me show my credentials, so I decided to do it in REST for security my code..
If have some form to solved this, please let me know.
If have other form to do it, I'll be happy to know.
Thanks advance.
The issue is likely that you need to write to the response object res.. In the updateMessage function the response is passed in. In order for data to be sent back to the browser you need to write to the response. I have a demo app which calls the weather channel to get the weather based on an intent, similar to what you are trying to do with your login function. Please take a look at this code
https://github.com/doconnor78/conversation-simple-weather/blob/master/app.js#L130
You will need to pass the original res (response) object into the appropriate function and then write data to the response (res) once you get it from the third party service.
I am trying to send a single https request to an API, with a bunch of data in JSON format.
However, when running the code, I first ran into the problem that everything was undefined. I commented out my loop, trying to parse the data I needed, and right now I'm just trying to console.log all of the data.
However, it seems to still be looping through stuff somehow despite not having a loop anywhere in my code anymore.
here is the code for my request:
function getCards() {
// make a request
var options = {
host: 'omgvamp-hearthstone-v1.p.mashape.com',
path: '/cards',
method: 'GET',
};
var req = https.request(options, function(res) {
console.log('STATUS ' + res.statusCode);
console.log('HEADERS ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(data) {
//console.log(data);
updateCardsCollection(JSON.stringify(data));
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
The weird thing is that the console.log(data) in the above code, logs out all of the data just fine.
function updateCardsCollection(data) {
var cardsRaw = [];
console.log("DATA");
console.log("===========================================================");
console.log(data.Classic);
}
Here "Classic" is one of the arrays of objects in the API.
Before implementing node, express, mongoose and jade. The following loop worked fine to parse through the data:
for(var key in data) {
for(var i = 0; i < data[key].length; i++) {
console.log(data[key][i].cardId);
However the above beginning of a loop would print out an undefined error as well.
Another strange problem I seem to be having with this code is when I run it with the loop commented out, with just the 3 console.logs in the 'updateCardsCollection' function. It logs those 3 lines a lot of times. Despite the function only being called once.
Any idea why this code is no longer working for getting my API data?
At first, you need to do JSON.parse instead of JSON.stringify while you try to update your collection, if you want work with objects, not with string.
But if you done with first, your code still not work, because in data handler you get chunked data, not full response. See https://nodejs.org/api/http.html#http_http_request_options_callback for it.
You must accumulate data like this:
const req = https.request(options, res => {
const data = [];
res.on('data', d => data.push(d));
res.on('end', () => updateCardsCollection(JSON.parse(data.join(''))));
})
I am very new to node.js and socket.io and I am trying to figure out how to read a JSON array from an external url, then parse it and display on the main page. Then I believe I use socket.io to keep that connection open and keep the JSON object up to date when a change occurs.
This is what I have so far for node.js.
var http = require("http");
var fs = require('fs');
var options = 'http://api.trakt.tv/user/watching.json/APIKEY/USERNAME';
var server = http.createServer(function(request, response){
console.log('Connection');
http.get(options, function(res){
var data = '';
res.on('data', function (chunk){
data += chunk;
});
res.on('end',function(){
var obj = JSON.parse(data);
console.log( obj );
})
});
response.end();
});
server.listen(8888);
When I connect to localhost:8888 I see the console show up with "connection" and then the console logs the contents of the JSON object. This is as far as I have managed to get. I would appreciate and help and pointers on how to get that JSON object displayed and styled on my index page and keep it up to date
TIA
Mark
Okay, now that I understand the problem, here is my answer. It's going to be a bit advice laden, as there is no true "right way" to do what you want. All we can be assured if is that yes, you are going to want to use WebSockets (or in this case socket.io, which can emulate websockets on older browsers).
Your current pull code is probably fine, though you're going to want to tweak that code to run on a timeout so that the latest JSON is pulled every so often. In addition, we want to keep the various moving parts of this seperate: Reading from the API/writing the cache, listening to the cache, and then feeding the cache out to connected clients:
var http = require("http");
var fs = require('fs');
var url = 'http://api.trakt.tv/user/watching.json/APIKEY/USERNAME';
var cacheFile = '/path/to/cachefile';
var connectedSockets = [];
function fetchJson() {
http.get(url, function(res) {
body = '';
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
fs.writeFileSync(cacheFile, body);
setTimeout(fetchJson, 1000); // Fetch it again in a second
});
})
}
fetchJson(); // Start fetching to our JSON cache
// Start watching our cache file
fs.watch(cacheFile, function(event, filename) {
if(event == 'change') {
fs.readFile(cacheFile, function(data) {
connectedSockets.forEach(function(socket) {
socket.emit('data', JSON.parse(data));
});
});
}
});
// Now setup our socket server
var io = require('socket.io').listen(8888);
io.sockets.on('connection', function(socket) {
connectedSockets.push(socket);
});
I don't handle disconnected here (you'll want to remove disconnected or err'ed sockets from the connectedSockets list), and I didn't actually run this...but it should give you an idea of where to head.
On the client, it should be a matter of simply:
var socket = io.connect('http://localhost:8888');
socket.on('data', function (data) {
// Data will contain your JSON object, do your DOM manip here
});