1.5.16 • Published 3 years ago

your-http v1.5.16

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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
ValueDescriptionType
socketTCP socketSocket
headersReturns all headersObject Property
rawHeadersAll raw headersArray
protocolRequest HTTP protocolstring
methodRequest methodstring
urlRequest endpointstring
bodyRequest bodyany
ipClient IPstring
queryReturns all query parametersObject Property
cookiesReturns all cookiesObject 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
ValueDescriptionType
socketTCP socketSocket
protocolResponse HTTP protocol (HTTP/1.1)string
statusCodeResponse status codenumber
statusMessageResponse status messagestring
headersReturns all headersObject Property
bodyResponse bodyany
serverServer namestring
lastModifiedLast change to the responseDate
canBeSent, wasSentValues used by the http server.boolean
status(number)Change status code of the responseFunction
write(string)Set content type to text and change response bodyFunction
json(object)Set content type to application/json and change response bodyFunction
cookiesReturns all cookiesObject Property
setCookie(name, value, settings: object)Adds a cookie to the response headersFunction
send(any)Automatically sets content type and changes bodyFunction
setHeaders(name, value)Adds a response headerFunction
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');
...
More features coming soon!

WARNING: I did not add any security features yet.

1.5.14

3 years ago

1.5.16

3 years ago

1.5.15

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.1

3 years ago

1.4.2

3 years ago

1.5.0

3 years ago

1.5.12

3 years ago

1.5.13

3 years ago

1.5.9

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.4.1

3 years ago

1.3.2

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.12

3 years ago

1.2.11

3 years ago

1.2.10

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago