zpeedi zpeedi - 1 year ago 60
Javascript Question

reading request object in node.js from localhost

I'm new to node.js and I'm trying out a few easy examples using localhost:XXXX.

I want to read my request object from node. I have a book and in the book they use cURL(some program) to comunicate with node instead of the browser. Is it possible to write something in the browser adress field and send it to localhost and have a request object sent to node that looks exactly like if I had a typed in a url to a server somewhere? OIf so, how do I write? Do I have to use cURL or something like it if i use localhost?

I'm very new to node and javascript so I dont't know if I'm using the right words. I have tried to search but I dont't think I know the right terms to search for.

This is my server code:
var port = 3000;
http.createServer(function (req, res) {
var url = parse(req.url);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n' + url );

When i write localhost:3000/hello.com in the adress field i was hoping i would get "Hello world" + "hello.com" in the browser but i get "hello world" + "[object object]"

Please help.

Answer Source

You can use your regular browser by testing it. In your URL address enter URL address that you have in your cURL address. For instance:


If you would like to have more sophisticated tool that gives you more information about request/response you can use tool like Postman for that

In your code use:

res.end('Hello World\n' + url.parse(req.url, true));

url is an object, you need to specify property or method that you are calling on it.

Here is an example on how to parse URL. Easy URL Parsing With Isomorphic JavaScript: