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.
我是node的初学者。js(实际上是今天才开始的)。其中一个基本概念对我来说不清楚,我在这里问的,也找不到。
Reading some tutorials on the web I wrote a client side & a server side code:
在网上阅读了一些教程后,我写了一个客户端和服务器端代码:
Server side (say server.js):
服务器端(比如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):
客户端(比如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.
现在,要运行服务器,我输入节点服务器。在终端或cmd提示中。当我浏览到127.0.0.1:1337时,它也会输出响应。
But, how to I run client.js? I could not understand how to run the client side code.
但是,如何管理client.js?我不明白如何运行客户端代码。
1 个解决方案
#1
10
Short answer: you can use the command
简短的回答:您可以使用这个命令
node client.js
to run your "client side" code, it will send one http request
要运行您的“客户端”代码,它将发送一个http请求。
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
在您的例子中,我认为它更像是一个“服务器”与另一个“服务器”对话,它们都位于后端,因为这就是节点。js是专为
#1
10
Short answer: you can use the command
简短的回答:您可以使用这个命令
node client.js
to run your "client side" code, it will send one http request
要运行您的“客户端”代码,它将发送一个http请求。
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
在您的例子中,我认为它更像是一个“服务器”与另一个“服务器”对话,它们都位于后端,因为这就是节点。js是专为