Client-side js to fetch node.js API calls - javascript

I am pretty new to node.js but I am using it to fetch data from an API. I want to return the response data to my client side js to be manipulated and injected into an html page. Pretty basic idea there, now do I make an HTTP request from the client side to my node.js to retrieve the JSON response text? How exactly do the client and server side communicate with eachother in this case?
factual.get(/t/places-us, {filters:{"locality":"seattle"}, {category_ids:{"$includes_any":[48, 50, 52]}}}, function (error, res) {
console.log(res.data);
});

You can use a framework like express.
Add a route to handle your client request.
Then build a response using your API response.

Related

Sending form data with Post while telling the server not to send a response

I’m posting form data using the fetch api to a web server which I don’t have control over. However, I don’t need the page in the response that the server sends back. Since I’m doing a lot of requests, I don’t want to overload the server.
Is it possible to tell the server to not send back the whole page in the response when making the request?
No. You the easiest thing to do would be to leverage HTTP Response Codes (see these here) to respond to a request and send little or no body along with it.
In express.js this would be doing:
router.post('/add', (req,res,next) => {
// Do logic-y things
// if success
res.sendStatus(200)
// else
res.sendStatus(500) // (or 400, or 403, or any other code that applies)
})

Use NodeJS server response with JavaScript

I have a NodeJS server, that receive HTML req, and I need him to resend the req to the client,
So I want to use res.writehead()....
But I don't know how to use the sent data in Javascript.
Thank you very much
Nathan

GitHub Webhook parse request object in Node.js

I would like to design and code a simple LaTeX CI environment. I need to parse request object that GitHub Webhook sends through HTTP POST for push and pull request events in Express (Node.js), but I can't find where it is located exactly.
GitHub API does not say where payload resides inside request object.
Also, how should JSON response be made? What HTTP code or JSON content should it have to communicate success or failure?
E.g.:
var app = express();
app.post('/build', function(req, res) {
// request parsing
// body
// send response
});
Thanks in advance for response.

WebSockets: Make javascript wait until server responds

My web application uses javascript to communicate with a server by HTTP requests. However the server software is changed so that instead of using HTTP requests, it uses WebSocket communication.
Rewriting the the entire web application to use open communication (i.e. WebSockets) instead of a request-respond mechanism entirely will be an incredibly tedious job, and I don't really have anything to gain on using WebSockets for the web application.
What I want to be able to achieve is to add a javascript module that communicates by WebSockets, but still in a request-respond manner. I guess this means that I have to "pause" the javascript until the server responds to my WebSocket messages? Because with HTTP requests my web application waited until the request was responded to.
So I guess it boils down to: How can I make javascript wait until the server sends a WebSocket message?
Cheers
EDIT: To specify, my Javascript code uses HTTP requests throughout the source code via a function I named "get(URL)". I want to modify this function so that it sends a WebSocket message, waits for the response and returns the response. Any ideas?
If I understand you correctly from the comments, you do in fact not desire to "pause" your Javascript, but merely to replace async ajax calls with websocket communication.
You would emit a message to the server, with a pre-defined key as it is in the server. You could then have some sort of payload in the message if you desire, and then have a callback function with the data you receive. Following is a sample where I did something similar for a simple chat application I created.
My client code:
socket.emit('join', options, function(res){
_printToChat(res);
});
Server code:
socket.on('join', function(roomname, fn){
socket.join(roomname);
fn('You joined ' + roomname);
});
This sends a messge with the key "join", and the callback is called with the response from server. In this case res in the client would be "You joined " + roomname.
You would likely do something like:
Client code:
socket.emit('getSomeResource', options, function(res){
// do something with response in res variable.
});
Server code:
socket.on('getSomeResource', function(yourOptions, fn){
fn(resourceToReturn);
});

Creating http echo server in node.js

What I need to do is simple node.js http echo server. However, servers that I found on node website and on some other places either don't work how I would like or I don't understand how they work.
I used this simple echo server and on my client side (written in java) I don't get the results I would want to.
var http = require('http');
http.createServer(function(request,response){
response.writeHead(200);
request.pipe(response);
}).listen(8080);
I would like my server to make a response consisting of response header and response body where I would have the whole http request that my client sent and got back. Client side is measuring time from sending a request and getting a whole response back. Client is also writing a response body to a file.
When I try using something like
response.write(request.url);
I get the request.url in the response body and it works but I would like to have the whole http request inside.
Any help would be appreciated.
use:
response.write(JSON.stringify(request.headers))
Add response.end() when done:
http.createServer(function(request,response){
response.writeHead(200)
request.pipe(response)
response.end()
}).listen(8080)
well you are trying to pipe the request to the response which doesn't make sense and doesn't work. I'd advice you to use the connect or express module. Then you can do this; response.status(200).send("Very Awesome").end().

Categories