وحدة HTTP Node.js
مثال
قم بإنشاء خادم يستمع إلى المنفذ 8080 بجهاز الكمبيوتر الخاص بك.
عند الوصول إلى المنفذ 8080 ، اكتب "Hello World!" العودة كرد:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello
World!');
res.end();
}).listen(8080);
التعريف والاستخدام
توفر وحدة HTTP طريقة لجعل Node.js ينقل البيانات عبر HTTP (بروتوكول نقل النص التشعبي).
بناء الجملة
بناء الجملة لتضمين وحدة HTTP في تطبيقك:
var http = require('http');
خصائص وطرق HTTP
Method | Description |
---|---|
createClient() | Deprecated. Creates a HTTP client |
createServer() | Creates an HTTP server |
get() | Sets the method to GET, and returns an object containing the user's request |
globalAgent | Returns the HTTP Agent |
request() | Returns an object containing the user's request |