Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/slaveofcode/grpc-restful-example
Example implementation of Restful service with gRPC interaction between internal services (NodeJS)
https://github.com/slaveofcode/grpc-restful-example
grpc grpc-client grpc-node grpc-server restful-api
Last synced: about 8 hours ago
JSON representation
Example implementation of Restful service with gRPC interaction between internal services (NodeJS)
- Host: GitHub
- URL: https://github.com/slaveofcode/grpc-restful-example
- Owner: slaveofcode
- License: mit
- Created: 2019-10-23T08:26:52.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-11T14:54:22.000Z (over 3 years ago)
- Last Synced: 2024-04-24T05:36:52.685Z (7 months ago)
- Topics: grpc, grpc-client, grpc-node, grpc-server, restful-api
- Language: JavaScript
- Size: 112 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gRPC Client-Server Example with RESTful for the Front-end
Example implementation of gRPC bwtween the internal microservices communications. The original request from the front-end is still using a Restful.
**Note:**
*This is just an example project, for those who wants to learn about gRPC on NodeJs.*The illustration below is about how this project behave, so you may get the idea about how it's work under the hood.
```+----------------------+
|DB| |
+ +--+ +--------------+ +------+
| | | Products | | |
| | +--------------+ +<---+ |
| | | | |
| +----------------------+ | | Queries
| | |
| | |
[Restful] | |
+-----------------+ HTTP Req +----------------+ RPC Calls +------------+-v+
| +------+----->+ +------------> |
| External Client | | | Client Service | | Server App |
| <------+------+ <------------+ |
+-----------------+ HTTP Resp +----------------+ +---------------+
|
|
|
|
|
+```
## Data Flow
1. HTTP Request coming from the front-end or client via **CURL** or another RESTful client (e.g. Insomnia, Postman, etc)
2. **Client Service** requesting data through Rpc to the **Server App**
3. **Server App** then doing a DB query and returning the result back
4. **Client Service** got the result and return back to the original front-end or client.
## Project Preparation
1. Clone this project
2. There are 3 subdirectories; `server` is a server of gRPC; `client` is a server of client which provide a restful service to the user, and `protos` holds the `.proto` file for *product*.
3. Go to `server` directory and install dependencies via
```
$ npm i
```4. Do the same step on the `client` directory
5. Create a new Postgres database called `grpc_products` (you may configure the username / password of the DB later on `server/knexfile.js`)
6. Running the migration from the server directory
```
$ npm run migrate
```7. Also the run the seeds
```
$ npm run seed
```## Run the gRPC server
Go to `server` directory and run `npm start`. If everything Ok you will see a message like
```
> gRPC server running at http://127.0.0.1:1831```
## Run the Restful server (Client of gRPC)
go to `client` directory and run `npm start`. You should see a message like this
```
> Server run at 3000
```## Testing RESTful request via CURL
### List Products
```
$ curl http://127.0.0.1:3000/api/products{
"products": [
{
"id": 1,
"name": "pencil",
"price": "1.99"
},
{
"id": 2,
"name": "pen",
"price": "2.99"
}
]
}
```### Read Product
```
$ curl http://127.0.0.1:3000/api/products/1{
"id": 1,
"name": "pencil",
"price": "1.99"
}
```### Create new Product
```
$ -X POST -d '{"name": "lamp", "price": "18.8"}' -H "Content-Type: application/json" http://127.0.0.1:3000/api/products{"status":"Created"}
```### Update Product
```
$ curl -X PUT -d '{"name": "Spoon", "price": "34.53"}' \
-H 'Content-Type: application/json' http://127.0.0.1:3000/api/products/3{"status":"Updated"}
```### Delete Product
```
$ curl -X DELETE http://127.0.0.1:3000/api/products/3{"status":"Deleted"}
```## LICENSE
MIT ([more](LICENSE))