Using NODE.js I am trying to develop a website which sends request to some third-party server, and the response being in JSON format, what is the best way rather the fastest way to filter the data directly ? Suppose i want to query certain books of a specific author, what is the best practice ?
Should i handle this in callback ? and then parse the entire response object ?
OR Should i filter the response while receiving itself in the stream using pipe ?
NOTE that i am intending to develop a website which will have to answer users in real-time, so time is precious here.
I think you can use callback to retrieve JSON data and them use websocket to pass the filtered data to the client in real time.
http://nodejs.org/api/http.html#http_http_get_options_callback
http.get("http://www.google.com/index.html", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
You can use socket.io for node.js
http://socket.io/
Related
I have a node.js server running and is executing what I want it to do, create an excel document with data fetched with Axios from an API, now, I want to allow a user to input a string on my HTML and then send that string to my web server, and perform an Axios request to the API that I am consuming. How can I do that? Should I use a POST request, a PUT request, or anything else?
Thanks!
You should be able to send data from your client to the server via the request body regardless of the request method whether GET/POST/PUT.
fetch call would look like this
// just a simple get request
fetch('/example-endpoint', {
method: 'POST',
body: {
data: 'String to be sent'
}
});
And on your node express server
app.post('/example-endpoint', (req, res) => {
const data = req.body.data;
// do stuff with your data here.
})
GET request method is used for getting data, while POST and PUT are used for creating and updating data, so use them as you need them.
POST request to your server, like uh https://myserver.com/api/${userString}
something like that
I have to display data of mqtt channel to the html page. I have prepared a node js code for the same as following :
var mqtt = require('mqtt');
client = mqtt.createClient(1883, 'mqtt.beebotte.com',
//Authenticate with your channel token,
{username: 'token:TOKEN_KEY', password: ''});
client.on('message', function (topic, message) {
console.log('topic: ' + topic + ' payload: ' + message);
});
client.subscribe('AppTeamDemo/GpsDataUpload');
client.subscribe('AppTeamDemo/EventDataUpload');
client.subscribe('AppTeamDemo/CommToZiggi');
client.subscribe('AppTeamDemo/CommFromZiggi');
And i am able to get data which i need to parse using JSON formatter and have to display it in html page.
I am confused as I am little new to all of this, how would I be able to use nodejs variable to the html page as it is a server side javascript.
How would I be able to connect with mqtt channel because every time it is saying mqtt is not defined.
I preferred not to use node and subscribe this channel in html page by using mqtt and by parsing the JSON data, I want to display latitude and longitude in google map. How would I be able to connect and get JSON response at front-end side ?
Please guide me. Thanks in advance.
Rather than write your own MQTT to Web bridge have you considered using a MQTT broker that supports WebSockets?
If you use the Paho library you can subscribe to the topics you are interested directly from javascript in the page.
Problem is when i try to use this code to get the log from tmi.twitch.tv api using url: http://tmi.twitch.tv/hosts?include_logins=1&target=70219146 i get Systax Error Unexpected Token. The code is:
$(document).ready(function() {
$.getJSON("http://tmi.twitch.tv/hosts?include_logins=1&target=70219146&callback=?", function (data) {
console.log(data.hosts)
});
})
I can get the data using php and json array like this:
$json_array = json_decode(file_get_contents('http://tmi.twitch.tv/hosts?include_logins=1&target=70219146'), true);
echo $json_array['hosts']['0']['host_login']."</br>";
But isnt there a way to use do this in html? thanks
You are trying to request regular JSON as JSONp (&callback=? activates jQuery's JSONp request method) which actually embeds the response into a <script> to execute it. However, the twitch API still returns JSON which is not valid JavaScript. Unless there is a way to make tmi.twitch.tv return valid JSONp, there is no way to do this directly from JavaScript unless you use a proxy like http://crossorigin.me/.
I'm have a REST API service, written on Node.js + Express, which currently support responses in JSON format.
Now, I need to add support for JSONP responses too, so everyone may get it by adding alt=JSONP to the request string.
So, my question is, which is the best way to do so?
Possible, there is an pattern, or living example?
Thanks!
The officially supported method is as follows --
First, set your JSONP callback parameter name:
app.set('jsonp callback name', 'callback');
Then, change all instances of res.json or res.send to res.jsonp.
For example, res.jsonp(500, { error: 'message' })
If the callback parameter is supplied in a request, the response will be wrapped as JSONP using the supplied function name, but if it isn't, it'll just return JSON.
Afterwards, this returns JSONP:
GET /myapi/v1/users?callback=responseCallback
This does not:
GET /myapi/v1/users
Reference http://expressjs.com/4x/api.html#res.jsonp
So, I am stuck here and feel like banging my head.
My problem is I query a database by ajax and returned its response in json form. Now I have this response in the form of javascript variable but I want to pass it to a php variable for further use.
How can I do that ??
function getJson(url){
return JSON.parse($.ajax({
type:'GET',
url: url,
dataType:'json',
global:false,
async:false,
success: function(data){
return data;
}
}).responseText);
}
Here how do I pass data to php ??
FIrstly I gave you -1 for not learning the basics!
- You can send client values to server (php) as querystring
example: /server_link?json=JSON_VAL
(not recommended for whole json string)
- Send it as post body (look for jquery ajax post) and parse it with php json library.
- if you have just single level json ({name:'John', sname:'Smith)} you can send it to php server(as mentioned before ajax post) and read as request variable in php ($_POST, $_...).
As I never programmed in php you'll have to do it you're self.
A again: LEARN THE BASICS (client side, server side, post, get,...).