# your-http

> A simple http server made for fun

Latest version **1.5.16** (published 2021-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install your-http
pnpm add your-http
yarn add your-http
bun add your-http
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.16 |
| Published | 2021-08-01 |
| First published | 2021-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 34.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | TerThesz |
| Maintainers | terthesz |
| Keywords | http, server, web server, web |

## Links

- npm: https://www.npmjs.com/package/your-http
- Repository: https://github.com/TerThesz/your-http
- Homepage: https://github.com/TerThesz/your-http#readme
- Issues: https://github.com/TerThesz/your-http/issues
- npm.io page: https://npm.io/package/your-http

## Dependencies (8)

- [net](https://npm.io/package/net.md) ^1.0.2
- [glob](https://npm.io/package/glob.md) ^7.1.7
- [path](https://npm.io/package/path.md) ^0.12.7
- [nodemon](https://npm.io/package/nodemon.md) ^2.0.12
- [ts-node](https://npm.io/package/ts-node.md) ^10.1.0
- [typescript](https://npm.io/package/typescript.md) ^4.4.0-dev.20210722
- [@types/glob](https://npm.io/package/@types/glob.md) ^7.1.4
- [@types/node](https://npm.io/package/@types/node.md) ^16.4.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.5.16 (latest) — 2021-08-01
- 1.5.15 — 2021-07-31
- 1.5.14 — 2021-07-31
- 1.5.13 — 2021-07-31
- 1.5.12 — 2021-07-31
- 1.5.9 — 2021-07-31
- 1.5.4 — 2021-07-31
- 1.5.3 — 2021-07-31
- 1.5.1 — 2021-07-31
- 1.5.0 — 2021-07-31
- 1.4.2 — 2021-07-31
- 1.4.1 — 2021-07-29
- 1.4.0 — 2021-07-29
- 1.3.6 — 2021-07-29
- 1.3.5 — 2021-07-29
- … 18 more at https://npm.io/package/your-http/versions

## README

<div style="display: flex;">
  <div>
    <h1 style="margin: 0; padding: 0">Package: your-http</h1>
    <h6 style="margin-top: 0; padding: 0; margin-bottom: 25px;">(Because you're the one using it).</h6>
  </div>

  <div style="margin: 10px 0 0 auto;">
    <img style="align:left;" src="https://github.com/TerThesz/your-http/actions/workflows/publish.yaml/badge.svg" />
    <img src="https://github.com/TerThesz/your-http/actions/workflows/test.yaml/badge.svg" />
    <img src="https://img.shields.io/npm/v/your-http?label=version" />
    <img src="https://img.shields.io/github/license/TerThesz/your-http" />
  </div>
</div>

<hr style="background-color: gray; opacity: 0.4; height: 2px; margin-top: 15px; margin-bottom: 40px;" />

### 1. Installation

Install package using:
``` bash
npm i your-http

or

yarn add your-http
```

### 2. Usage <br/>

##### 1. Importing

###### JS
``` javascript
  const http = require('your-http');
```

###### TS
``` typescript
  import http from 'your-http';
```

##### 2. Route Examples

###### JS
``` javascript
  const http = require('your-http');
  const router = new http.Router();

  router.get('/', (req, res, next) => {
    /* Code... */
  });

  exports.default = router;
```

###### TS
``` typescript
  import { Router } from 'your-http';
  const router = new Router();

  router.get('/', (req, res, next) => {
    /* Code... */
  });

  export = router;
```
<br/>

##### 3. How to use the Router

##### 3.A Methods

###### Usage:
`router.get('/', (req, res, next) => {});`

###### Methods:
`.get(), .post(), .delete(), .put(), .update()`

##### 3.B Arguments

###### All arguments:
`req, res, next`

##### 3.B A Request (req)
###### All request information is stored in here.

###### Request object:
###### How to use it: [Click here](#3-detailed-usage)

| Value | Description | Type |
| ------|:------------|-----:|
| socket | TCP socket | Socket |
| headers | Returns all headers | Object Property |
| rawHeaders | All raw headers | Array |
| protocol | Request HTTP protocol | string |
| method | Request method | string |
| url | Request endpoint | string |
| body | Request body | any |
| ip | Client IP | string |
| query | Returns all query parameters | Object Property |
| cookies | Returns all cookies | Object Property |

###### Only useful functions:
``` javascript
req.headers;

req.rawHeaders;

req.body;

req.ip;

req.query;

req.cookies;
```

##### 3.B B Response (res)
###### All response information is stored in here.

###### Response object:
###### How to use it: [Click here](#3-detailed-usage)

| Value | Description | Type |
| ------|:------------|-----:|
| socket | TCP socket | Socket |
| protocol | Response HTTP protocol (HTTP/1.1) | string |
| statusCode | Response status code | number |
| statusMessage | Response status message | string |
| headers | Returns all headers | Object Property |
| body | Response body | any |
| server | Server name | string |
| lastModified | Last change to the response | Date |
| canBeSent, wasSent | Values used by the http server. | boolean |
| status(number) | Change status code of the response | Function |
| write(string) | Set content type to text and change response body | Function |
| json(object) | Set content type to application/json and change response body | Function |
| cookies | Returns all cookies | Object Property |
| setCookie(name, value, settings: object) | Adds a cookie to the response headers | Function |
| send(any) | Automatically sets content type and changes body | Function |
| setHeaders(name, value) | Adds a response header | Function |

###### Only useful functions:
``` javascript
res.headers;

res.status(number);

res.write(string);

res.json(object);

res.send(any);

res.setCookie(name, value, settings);

res.setHeader(name, value);
```

##### DO NOT MANUALLY CHANGE RESPONSE INFORMATION. Use response functions instead ( All functions displayed above );

##### 3.B C NextFunction (next)
###### If called, next router will run after the current one finishes.

###### Type: Function. No arguments or values.

##### 4. How to create a server
###### Usage
``` javascript
  http.createServer(router1, router2...).listen(port, ip[optional], callback[optional]);
```
###### Example
``` javascript
  http.createServer(router1).listen(3000, '0.0.0.0', () => 
    console.log('🏃 on port 3000.'));
```
###### Tip: Use ip: '0.0.0.0' to get IPv4 from req.ip
<br/>

### 3. Detailed Usage

##### 1. Working with cookies

###### Getting all request cookies
```javascript
...
req.cookies;
...
```
###### Adding response cookies
```javascript
...
res.setCookie('name', 'value', { Settings: true });
...
```

##### 2. Query & body

###### Getting request body
``` javascript
...
req.body;
...
```
###### Returning response body
``` javascript
...
res.functionName/* send or write or json or status */(value);
...
```
###### Getting query parameters
``` javascript
...
req.query;
...
```
##### Headers

###### Getting request headers
``` javascript
...
req.headers;
...
```
###### Adding a response header
``` javascript
...
res.setHeader('name', 'value');
...
```

<br/>
<br/>

###### More features coming soon!

#### WARNING: I did not add any security features yet.

---
_Source: https://npm.io/package/your-http · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
