3 const util = require('util');
4 const internalUtil = require('internal/util');
5 const EventEmitter = require('events');
8 exports.IncomingMessage = require('_http_incoming').IncomingMessage;
11 const common = require('_http_common');
12 exports.METHODS = common.methods.slice().sort();
15 exports.OutgoingMessage = require('_http_outgoing').OutgoingMessage;
18 const server = require('_http_server');
19 exports.ServerResponse = server.ServerResponse;
20 exports.STATUS_CODES = server.STATUS_CODES;
23 const agent = require('_http_agent');
24 const Agent = exports.Agent = agent.Agent;
25 exports.globalAgent = agent.globalAgent;
27 const client = require('_http_client');
28 const ClientRequest = exports.ClientRequest = client.ClientRequest;
30 exports.request = function(options, cb) {
31 return new ClientRequest(options, cb);
34 exports.get = function(options, cb) {
35 var req = exports.request(options, cb);
40 exports._connectionListener = server._connectionListener;
41 const Server = exports.Server = server.Server;
43 exports.createServer = function(requestListener) {
44 return new Server(requestListener);
50 function Client(port, host) {
51 if (!(this instanceof Client)) return new Client(port, host);
52 EventEmitter.call(this);
54 host = host || 'localhost';
58 this.agent = new Agent({ host: host, port: port, maxSockets: 1 });
60 util.inherits(Client, EventEmitter);
61 Client.prototype.request = function(method, path, headers) {
64 options.host = self.host;
65 options.port = self.port;
66 if (method[0] === '/') {
71 options.method = method;
73 options.headers = headers;
74 options.agent = self.agent;
75 var c = new ClientRequest(options);
76 c.on('error', function(e) {
77 self.emit('error', e);
79 // The old Client interface emitted 'end' on socket end.
80 // This doesn't map to how we want things to operate in the future
81 // but it will get removed when we remove this legacy interface.
82 c.on('socket', function(s) {
83 s.on('end', function() {
85 var ret = self._decoder.end();
87 self.emit('data', ret);
95 exports.Client = internalUtil.deprecate(Client, 'http.Client is deprecated.');
97 exports.createClient = internalUtil.deprecate(function(port, host) {
98 return new Client(port, host);
99 }, 'http.createClient is deprecated. Use http.request instead.');