Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nwtgck/promise-http-server-npm
Promise-based HTTP server for JavaScript/TypeScript: using accept(), easy to use in async-await context
https://github.com/nwtgck/promise-http-server-npm
accept http-server http2 http2-server javascript node nodejs promise typescript
Last synced: 14 days ago
JSON representation
Promise-based HTTP server for JavaScript/TypeScript: using accept(), easy to use in async-await context
- Host: GitHub
- URL: https://github.com/nwtgck/promise-http-server-npm
- Owner: nwtgck
- License: mit
- Created: 2018-09-11T23:35:01.000Z (about 6 years ago)
- Default Branch: develop
- Last Pushed: 2024-06-16T09:55:57.000Z (5 months ago)
- Last Synced: 2024-10-26T15:29:48.846Z (20 days ago)
- Topics: accept, http-server, http2, http2-server, javascript, node, nodejs, promise, typescript
- Language: TypeScript
- Homepage:
- Size: 200 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# promise-http-server
[![npm](https://img.shields.io/npm/v/promise-http-server.svg)](https://www.npmjs.com/package/promise-http-server)
[![Build Status](https://travis-ci.com/nwtgck/promise-http-server-npm.svg?token=TuxNpqznwwyy7hyJwBVm&branch=develop)](https://travis-ci.com/nwtgck/promise-http-server-npm)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.```js
// 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(`Your path: ${req.url}
\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](https://nodejs.org/api/http.html).
`promiseServer.server` is also pure Node.js object.Here are the types of those objects.
* `req`: [http.IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
* `res`: [http.ServerResponse](https://nodejs.org/api/http.html#http_class_http_serverresponse)
* `promiseServer.server`: [http.Server](https://nodejs.org/api/http.html#http_class_http_server)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` |