Skip to content

Promise-based HTTP server for JavaScript/TypeScript: using accept(), easy to use in async-await context

License

Notifications You must be signed in to change notification settings

nwtgck/promise-http-server-npm

Repository files navigation

promise-http-server

npm Build Status

Promise-based HTTP server

Install

npm i -S promise-http-server

Create a 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}`);
    }
  }
})();

Pure objects in http module

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.

Pure objects allow you to create a server in your familiar way.

HTTP, HTTPS, HTTP/2, Secure HTTP/2

Here are classes to handle HTTP-related protocols.

HTTP PromiseHttpServer
HTTPS PromiseHttpsServer
HTTP/2 PromiseHttp2Server
Secure HTTP/2 PromiseHttp2SecureServer

About

Promise-based HTTP server for JavaScript/TypeScript: using accept(), easy to use in async-await context

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published