19

I am a beginner in node.js (infact started just today). One of the basic concepts is not clear to me, which I am asking here & couldn't find on SO.

Reading some tutorials on the web I wrote a client side & a server side code:

Server side (say server.js):

var http = require('http'); //require the 'http' module

//create a server
http.createServer(function (request, response) {
  //function called when request is received
  response.writeHead(200, {'Content-Type': 'text/plain'});
  //send this response
  response.end('Hello World\nMy first node.js app\n\n -Gopi Ramena');
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

Client side (say client.js):

var http=require('http');

//make the request object
var request=http.request({
  'host': 'localhost',
  'port': 80,
  'path': '/',
  'method': 'GET'
});

//assign callbacks
request.on('response', function(response) {
   console.log('Response status code:'+response.statusCode);

   response.on('data', function(data) {
     console.log('Body: '+data);
   });
});

Now, to run the server, I type node server.js in the terminal or cmd prompt. & it runs successfully logs the message in the console & also outputs the response when I browse to 127.0.0.1:1337.

But, how to I run client.js? I could not understand how to run the client side code.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
gopi1410
  • 6,567
  • 9
  • 41
  • 75
  • 3
    Node is server side not client side. Take a look at this. http://stackoverflow.com/questions/5168451/javascript-require-on-client-side – Larry Battle Jun 18 '12 at 18:24
  • 2
    Not sure where you found client.js or what you expect it to do, but that is not something you could put in a browser. If by client, you mean another Node.js application, sure. I think the tutorial you found was instructing you on how to write a server that accesses HTTP resources on other servers. – Brad Jun 18 '12 at 18:24
  • Where did you find that `client.js`? Did you write it yourself? What are you trying to achieve with that? – jsalonen Jun 18 '12 at 18:24
  • so how does the client communicate with the node.js server. I mean the server cant just keep on outputing values at specified time or based on specified urls. – gopi1410 Jun 18 '12 at 18:26
  • @jsalonen http://www.slideshare.net/Sudar/introduction-to-nodejs-13354652 – gopi1410 Jun 18 '12 at 18:27
  • Judging from the slides, he is just pointing out that you can use node.js either as a client or as a server. You don't need to use node.js in both roles if you don't want to - and in fact, if you want to implement web applications, you should use node.js only as a server. – jsalonen Jun 18 '12 at 18:32
  • The title was very misleading. – PascalVKooten Feb 12 '16 at 08:45
  • It is good question and it is documented in the NodeJS version 11.8.0 [Client Example](https://nodejs.org/api/http2.html#http2_client_side_example) and in [http.request](https://nodejs.org/api/http.html#http_class_http_clientrequest); but it is more easy to use [XHR module](https://www.npmjs.com/search?q=xmlhttprequest) or [AXIOS](https://www.npmjs.com/package/axios) that can be used isomorphically in browser and server. – Yones Jan 30 '19 at 22:34

1 Answers1

10

Short answer: you can use the command

node client.js

to run your "client side" code, it will send one http request

Regarding to what's server side and what's client side, it's really depends on the context.

Although in most cases, client side means the code running on your browser or your mobile phone app, server side means the "server" or "back end" your browser or your mobile phone is talking to.

In your case, I think it's more like one "server" talks to another "server", and they are both on the back end, since that's what node.js is designed for

xvatar
  • 3,229
  • 17
  • 20
  • 1
    @gopi1410 as Larry said, if you want your client.js talks to your server.js, you should change the port of your client.js to 1337 – xvatar Jun 18 '12 at 19:04
  • yup I changed 'host' to '127.0.0.1' and 'port' to 1337, but still when I run `node client.js`, it doesn't show any error, but also it doesnot output anything in the console. Shouldn't is show the response status code?? – gopi1410 Jun 18 '12 at 19:07
  • 3
    @gopi1410 you need to add `request.end();` in the end – xvatar Jun 18 '12 at 19:20