your-http
v1.5.16
Published
A simple http server made for fun
Downloads
11
Readme
1. Installation
Install package using:
npm i your-http
or
yarn add your-http
2. Usage
1. Importing
JS
const http = require('your-http');
TS
import http from 'your-http';
2. Route Examples
JS
const http = require('your-http');
const router = new http.Router();
router.get('/', (req, res, next) => {
/* Code... */
});
exports.default = router;
TS
import { Router } from 'your-http';
const router = new Router();
router.get('/', (req, res, next) => {
/* Code... */
});
export = router;
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
| 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:
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
| 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:
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
http.createServer(router1, router2...).listen(port, ip[optional], callback[optional]);
Example
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
3. Detailed Usage
1. Working with cookies
Getting all request cookies
...
req.cookies;
...
Adding response cookies
...
res.setCookie('name', 'value', { Settings: true });
...
2. Query & body
Getting request body
...
req.body;
...
Returning response body
...
res.functionName/* send or write or json or status */(value);
...
Getting query parameters
...
req.query;
...
Headers
Getting request headers
...
req.headers;
...
Adding a response header
...
res.setHeader('name', 'value');
...