-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
39 lines (30 loc) · 912 Bytes
/
request.js
File metadata and controls
39 lines (30 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module.exports = class Request {
constructor(host, schema, path) {
this.host = host;
this.schema = schema;
this.path = path;
this.header = {
"Host": this.host,
"Accept-Encoding": "utf-8",
};
this.method = 'GET';
}
setHeader(name, value) {
this.header[name] = value;
}
setMethod(method = 'GET') {
this.method = method;
}
stringify() {
let joinPath = this.path.join('/');
joinPath = joinPath === "" ? "/" : joinPath;
const requestLine = `${this.method} ${joinPath} HTTP/1.1\r\n`;
let r = requestLine;
for(const name in this.header) {
r += `${name}: ${this.header[name]}\r\n`;
} r += '\r\n';
console.log('---------------request-----------------');
console.log(r);
return r;
}
};