https://github.com/pedramcode/rhttp-node
RHTTP NodeJS interface
https://github.com/pedramcode/rhttp-node
Last synced: 2 months ago
JSON representation
RHTTP NodeJS interface
- Host: GitHub
- URL: https://github.com/pedramcode/rhttp-node
- Owner: pedramcode
- License: mit
- Created: 2023-02-11T06:21:02.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-15T15:07:55.000Z (about 3 years ago)
- Last Synced: 2025-11-11T13:27:54.488Z (5 months ago)
- Language: TypeScript
- Homepage:
- Size: 62.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# RHTTP-node
RHTTP NodeJS interface
## Usage
First create RHTTP server and pass redis host and port to it
```ts
const server = new RHTTPServer("127.0.0.1", 6379);
```
The `RHTTPServer` class have a function for every HTTP method. Each method function has two parameters:
1. **Path**: It's the path of endpoint
2. **Callback**: It's callback function to handling requests
```ts
server.get('/', (req, res) => {
const response_data = {
msg: 'Hello User!',
};
return res.status(200).contentType('application/json').send(JSON.stringify(response_data));
});
```
The `Callback` parameter is a function with two parameters:
* **request**: Contains HTTP request data such as body, query params, etc.
* **response**: An instance of `Response` class that you can modify and send response to user using it.
### Response methods:
* **status**: Sets HTTP status code and status message
* `res.status(200)`
* **contentType**: Sets HTTP content type
* `res.contentType("application/json")`
* **header**: Append HTTP header to response
* `res.header({"X-Custom-Header": "Header Value"})`
* **send**: Finalizes response progress and make HTTP response string
* `res.status(200).contentType('application/json').send(JSON.stringify(response_data))`
> **Note**
> You need to return `send()` method result to callback function to continue progress.
At the end server needs to listen to Redis for receiving incoming requests:
```ts
server.listen((err) => {
if (err) {
console.error(err);
} else {
console.info('Server is ready');
}
});
```
## License
This project is licensed under the terms of the MIT License. See the [LICENSE](LICENSE.txt) file for details.