Compatibilidade APIs Node.js - HTTP

A biblioteca http no Node.js é um módulo central que fornece utilitários para criar servidores e clientes HTTP. Ela permite que os desenvolvedores manipulem requisições e respostas HTTP, tornando-se essencial para a construção de aplicações e serviços web.

/**
* An example of using the Node.js HTTP API in an Azion Edge Function.
* Support:
* - Extended by library `stream-http`
* @module runtime-apis/nodejs/http/main
* @example
* // Execute with Azion Bundler:
* npx edge-functions build
* npx edge-functions dev
*/
import http from "node:http";
/**
* An example of using the Node.js HTTP API in an Azion Edge Function.
* @param {*} event
* @returns {Promise<Response>}
*/
const main = async (event) => {
// this is a workaround to make the http module work in the browser
const protocol = event.request.headers.get("x-forwarded-proto") || "http";
globalThis.location = {
protocol,
};
return new Promise((resolve, reject) => {
http
.request("https://jsonplaceholder.typicode.com/todos/1", (res) => {
console.log("Got response: " + res.statusCode);
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
console.log("No more data in response.");
console.log("BODY: " + data);
resolve(new Response(JSON.stringify(data)));
});
res.on("error", (err) => {
console.error(err);
reject(new Response("Error occurred"));
});
})
.end();
});
};
export default main;

Contribuidores