私はNode.jsを学習しています。サーバとクライアントを作成しました。.jsファイルですが、私はいくつかのことを理解していません。たとえば、webserver.jsファイルでは、pathname
の使用方法はわかりません。同様に、client.js
ファイルでは、data
とpath
は何ですか?このコードのクライアント、パス、およびデータとは何ですか?
もし私がそれの基本についてお読みにならなければならないと思うなら、できるだけ私に有用なリンクを提供してください。私は見つけようとしたが働かなかった。
webserver.js
var fs=require('fs');
var url=require('url');
var http=require('http');
http.createServer(function(request, response){
var pathname=url.parse(request.url).pathname;
console.log("Pathname: "+pathname+"Request.url: "+request.url);
fs.readFile(pathname.substr(1), function(err, data){
if(err){
console.log("Error reading.");
response.writeHead(400, {'content-type' : 'text/html'});
}else{
response.writeHead(200, {'content-type' : 'text/html'});
response.write(data.toString());
}
response.end();
});
}).listen(8081);
console.log("Server is running.");
client.js
var http=require('http');
var options={
host: 'localhost',
port: '8081',
path: '/index.html'
};
var callback=function(response){
var body='';
response.on('data', function(data){
body+=data;
});
response.on('end', function(){
console.log("Data received.");
});
}
var req=http.request(options, callback);
req.end();
元のコード源泉ここにある:Code
'あなたはcan'場合は私に便利なリンクを提供してください - https://nodejs.org/en/docs/ –
Iそこに必要なものは見られませんでした。あなたは少し指導できますか? –
[これは助けてください](https://nodejs.org/api/url.html) –