Promise-based HTTP server
npm i -S promise-http-server
Although this project is written in TypeScript, you can also use JavaScript.
Here is a simple HTTP server written in JavaScript.
// JavaScript
const {PromiseHttpServer} = require("promise-http-server");
(async ()=>{
const promiseServer = new PromiseHttpServer();
const port = 8899;
// Listen on the port
await promiseServer.listen(port);
console.log(`Listening on ${port}...`);
while(true) {
try {
// Wait for request
const {req, res} = await promiseServer.accept();
// Write request path and end
res.end(`<h1>Your path: ${req.url}</h1>\n`);
} catch (err) {
// Print error
console.error(`on-error: ${err}`);
}
}
})();
req
and res
above are pure Node.js object in http
module.
promiseServer.server
is also pure Node.js object.
Here are the types of those objects.
req
: http.IncomingMessageres
: http.ServerResponsepromiseServer.server
: http.Server
Pure objects allow you to create a server in your familiar way.
Here are classes to handle HTTP-related protocols.
HTTP | PromiseHttpServer |
HTTPS | PromiseHttpsServer |
HTTP/2 | PromiseHttp2Server |
Secure HTTP/2 | PromiseHttp2SecureServer |