Sonic is a high-performance 100% open source, config based free API Gateway for Nodejs. All source code is available under the MIT License on GitHub.
Its core functionality is to create an API that acts as an aggregator of many microservices into single endpoints.
- A single API that exposes many.
- Merge the content and modify of several APIs into one.
- Device detection (mobile, desktop).
- HTTP Caching(Redis, memory).
- Unlimited endpoints and backends.
- Secure the transport.
- Error handling.
- URL patterns and variables
- Request body validation
- Use middlewares(Response, Request, Error, and your own).
- Simple JSON configuration.
This is a Node.js module available through the npm registry.
Using npm:
$ npm install sonic-api-gateway --save
Using yarn:
$ yarn add sonic-api-gateway
Now that we have installed the Sonic API gateway module.
Let's create a Sonic project file.
app.js
const Sonic = require('sonic-api-gateway').Sonic
const Config = {
port : 3014, // Server will run on port 3014
routes: [
{
endpoint: '/posts/:id', // http://localhost:3014/posts/:id
method: 'get',
backend: [
{
target: 'https://jsonplaceholder.typicode.com/posts/{id}',
method: 'get',
response_key : "posts",
},
{
target: 'https://jsonplaceholder.typicode.com/posts',
method: 'get',
response_key : "all_posts",
},
// ...
]
},
// ...
]
}
new Sonic(Config);
Start Sonic server:
node app.js
Make a GET request (as we declare in config) with CURL:
curl -G http://localhost:3014/posts/1
{
version: String, // Optional
logs: Boolean, // Optional
port : Number, // Optional, Default 3000
debug: Boolean, // Optional, Default true, for logging
middlewares: Array, //Optional global middlewares in the format: (req, res, next) => next(), Default []
cache: [ // Optional
{
driver: String, // Required, redis | memory
host: String, // Optional
port: Number // Optional
}
// ...
],
routes: [
{
endpoint : String, // Required, ex: "/posts"
method : String, // Required, GET, POST, DELETE, PUT
cache: { // Optional,
driver: String, // Required, redis | memory,
ttl: Number, // Optional, 1 = 1 second
},
params : { // Optional
[key: String] : String
},
onResponse?(req: any, res: any, next: any, data: any, route: any): Function; // Optional
onRequest?(req: any, res: any, next: any): Function; // Optional
backend: [
{
target: String, // Required, ex: "https://jsonplaceholder.typicode.com/posts"
method: String, // Required, GET, POST, DELETE, PUT
response_key: String, // Optional
response_status : Number, // Optional
onResponse?(req: any, res: any, next: any, data: any, route: any): Function, // Optional
auth : Boolean, // Optional
onError?(req, res, next, proxyRoute, error) : Function // Optional
params : { // Optional
[key: String] : String
},
body: {
[key: string]: 'string' | 'files' | 'any' | 'number' | 'boolean'
}, // Required if request method is POST, PUT. ex: body : {name : 'string'}
body_method: String, // Default : rawdata. Possible: formdata, urlencoded, raw
headers: Object // Optional, set request header
childRoutes: Backend[] // Experimental
}
]
}
// ...
]
}
PORT
- Sonic server portVERSION
- Informing client about Sonic versionDEBUG
- Sonic debug modeLOGS
- Log incoming requests infoMIDDLEWARES
- MiddlewaresCACHE
- Caching responsesROUTES
- APIROUTES.ENDPOINT
- API endpointROUTES.METHOD
API methodROUTES.onResponse
- Handle responseROUTES.CACHE
ROUTES.BACKEND
ROUTES.BACKEND.TARGET
ROUTES.BACKEND.METHOD
ROUTES.BACKEND.RESPONSE_KEY
ROUTES.BACKEND.RESPONSE_STATUS
ROUTES.BACKEND.ONRESPONSE
ROUTES.BACKEND.AUTH
ROUTES.BACKEND.BODY
ROUTES.BACKEND.BODY_METHOD
ROUTES.BACKEND.HEADERS
ROUTES.BACKEND.CHILD_ROUTES
Start a server listening for connections. It will create simple http server with express.
Type: Number Default: 3000 Required: false
Example:
new Sonic({
port : 3014
// ...
})
curl -G http://localhost:3014
⚠️ If address allready in use server will not start
Seting response header for every response wich sending from server to client.
Type: String Default: 1.0 Required: false
Example:
new Sonic({
version: 2.0,
// ...
})
Debug styled server actions in your node.js console. It will log incoming requests, server status, caching servers status, invalid config error
Type: Boolean Default: true Required: false
Example:
new Sonic({
debug: true
// ...
})
Save request date and client ip into the file in root directory _log/{date)
Type: Boolean Default: false Required: false
Example:
new Sonic({
logs : true,
// ...
})
Use Express middlewares.
Type: Array:Function(req,res,next) Default: none Required: false
Example:
new Sonic({
middlewares: [
(req: Request, res: Response, next: NextFunction) => {
console.log(req.method)
next() // Don't miss
},
morgan(),
// ...
]
// ...
})
With caching option you can cache every response to a redis or disk memory. This caching technique applies to traffic between Sonic and your microservices endpoints.
Type: Array:Object (port?: Number, host?: String, driver: 'redis' || 'memory')
Default: none
Required: false
Possible cache drivers: redis | memory
Example:
new Sonic({
cache : [
{
driver: 'redis',
host: '1231.1.2.3.4',
port: 12701
},
{
driver: 'memory',
}
],
routes : [
{
endpoint: "/v1/test",
method: "get",
cache: {
driver: "memory",
},
backend: [
// ...
]
},
{
endpoint: "/v1/test/pets",
method: "get",
cache: {
driver: "redis",
},
backend: [
// ...
]
}
]
// ...
})
An array of endpoint objects offered by the gateway, all the associated backends and configurations.
Type: Array:Object Default: none Required: true
Example:
new Sonic({
routes : [
{
endpoint: "/v1/test",
method: "get",
backend: [
// ...
]
},
// ...
]
// ...
})
The resource URL you want to expose
Type: String Default: none Required: true Note: Endpoints starts with "/"
Example
new Sonic({
// ...
routes : {
endpoint : "/v1/test-endpoint"
// ...
}
// ...
})
curl http://localhost:3014/v1/test-endpoint
Request method, GET, POST, PUT or DELETE
Type: String Default: get Required: true Note: Possible : GET, POST, PUT, DELETE
Example
new Sonic({
// ...
routes : {
endpoint : "/v1/test-endpoint",
method : "get"
// ...
}
// ...
})
curl http://localhost:1001/v1/test-endpoint
Documentation in progress.