我是node.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.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);
});
});
现在是,要运行服务器,我在或cmd提示符中输入node server.js
。& it运行成功,将消息记录在控制台中,并在我浏览到127.0.0.1:1337时输出响应。
但是,我如何运行client.js呢?我无法理解如何运行客户端代码。
发布于 2012-06-19 02:55:06
简而言之:您可以使用以下命令
node client.js
为了运行你的“客户端”代码,它将发送一个http请求。
至于什么是server side
,什么是client side
,这实际上取决于上下文。
虽然在大多数情况下,client side
指的是在您的浏览器或手机应用程序上运行的代码,但server side
指的是您的浏览器或手机与之对话的“服务器”或“后端”。
在您的例子中,我认为这更像是一个“服务器”与另一个“服务器”对话,它们都在后端,因为这就是设计node.js的目的
https://stackoverflow.com/questions/11088720
复制相似问题