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().
Related
The npm-request library allows me to construct HTTP requests using a nice JSON-style syntax, like this.
request.post(
{
url: 'https://my.own.service/api/route',
formData: {
firstName: 'John',
lastName: 'Smith'
}
},
(err, response, body) => {
console.log(body)
}
);
But for troubleshooting, I really need to see the HTTP message body of the request as it would appear on the wire. Ideally I'm looking for a raw bytes representation with a Node.js Buffer object. It seems easy to get this for the response, but not the request. I'm particularly interested in multipart/form-data.
I've looked through the documentation and GitHub issues and can't figure it out.
Simplest way to do this is to start a netcat server on any port:
$ nc -l -p 8080
and change the URL to localhost in your code:
https://localhost:8080/v1beta1/text:synthesize?key=API_KEY
Now, any requests made will print the entire, raw HTTP message sent to the localhost server.
Obviously, you won't be able to see the response, but the entire raw request data will be available for you to inspect in the terminal you have netcat running
I figured out how to dump the HTTP message body with Request. In both cases, I'm just copying the same approach that request uses internally.
Multipart Form Uploads
req._form.pipe(process.stdout);
URL-encoded Forms
console.log(req.body);
You could try #jfriend00 suggestion an use a network sniffer like wireshark but as you're fetching an https URL this might not be the easiest route as it requires some setup to intercept TLS connections.
So maybe it would be enough turning on debug mode for the request module itself, you can do that by simply setting require('request').debug = true. As a third option you could go with the dedicated debug module for request here which allows you to view request and response headers and bodies.
I can think of a number of ways to see the bytes of the request:
Turn on debugging in the request module. There are multiple ways to do that documented here including setting NODE_DEBUG=request or require('request').debug = true or using the request-debug module.
Use a network sniffer to see what's actually being sent over the socket, independent of your node.js code.
Create your own dummy http server that does nothing but log the exact incoming request and send your same request to that dummy server so it can log it for you.
Create or use a proxy (like nginx) that can dump the exact incoming request before forwarding it on to its final destination and send the request to the proxy.
Step through the sending of the request in the debugger to see exactly what it is writing to the socket (this may be time consuming, particularly with async callbacks, but will eventually work).
you could use a nodejs server capable of logging the raw request/response string , then direct your request to that server
i gave an example using both http and https server - no dependencies
nodejs getting raw https request and response
I have a simple node http server set up currently, I'm trying to build the server so that it only accepts post requests from certain domains. I'm trying to get the domain of the server that made a post request to my node server but can't quite figure it out.
In my test environment, I am making the post request from localhost:3000 to my node server which is running on localhost:9220. I have been examining the req object but can't seem to find localhost:3000 mentioned anywhere in it.
http.createServer(function (req, res) {
if(req.method == 'POST')
{
// Here is where I want to find the domain of the server
// making the request
{
}).listen(9220);
It's probably simple but I am having trouble
thanks for the help!
There is no way to do this reliably.
If you make a cross-origin Ajax request, a browser will add an Origin header to it. If you submit a form, a browser might add a Referer header to it.
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.
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.
In PHP, getting cookies sent from a remote server is simply a matter of using cURL with some cookie handling option enabled.
If I was to treat my server as a client making requests to a remote server, I was wondering how this might be done in node.js? Would my server/app even be receiving these cookies? If so, how can I get the name and values of these cookies?
I've tried using the following node.js modules to no avail:
request (??? not part of functionality?)
tough-cookie (not part of functionality)
client-http (I get an empty array)
node-curl (I get an empty array)
Any pointers would be appreciated. Thanks!
You can use the http module that comes with Node.js
var http = require('http');
http.get('http://www.google.ca', function(res) {
console.log(res.headers['set-cookie']);
});
will give you all the cookies that google.ca would try to set on you when you visit.