mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-06 02:46:39 +00:00
1.3 KiB
1.3 KiB
HTTP(S) Module
HTTTP(S) IncomingMessage (request)
Making a request
https.request(
{
host: "www.website.com",
method: "GET", // POST, ...
path: "/page/?param=value"
},
(response) => { // respnse is IncomingMessage
// do stuff
}
).end();
Reqest Methods & Properties
IncomingMessage.headers
IncomingMessage.statusCode
IncomingMessage.statusMessage
IncomingMessage.url
HTTTP(S) ServerResponse (response)
Response Methods & Properties
ServerResponse.writeHead(statusCode[, statusMessage][, headers]);
HTTP(S) Server
Creating a server
const PORT = 8123;
// req is IncomingMessage
// res is ServerResponse
const server = http.createServer((req, res) => {
let body = "<html></html>"
res.writeHead(200, {
"Content-Length": Buffer.byteLength(body),
"Content-Type": "text/html; charset=utf8"
});
res.end(body);
});
server.listen(PORT);
Reading a request search params
const http = require("http");
const url = require("url");
const PORT = 8123;
const server = http.createServer((req, res) => {
let url = new URL(req.url, "http://" + req.headers.host);
let params = url.searchParams;
// ...
});
console.log("Listening on port: " + PORT);
server.listen(PORT);