REST
Last updated
Last updated
At the moment, code.store's platform supports a simple file-system-based routing of REST API endpoints.
REST endpoints are based on Express framework, so you can use all Express features to build your application. Using REST-based on Express you are able to create endpoints of different methods (such as GET, POST, PUT, DELETE, PATH…), define your route paths, handlers, work with route parameters, define your middlewares…
code.store platform simplifies the process of creating endpoints and their handlers using flexible configuration and provides an ability to generate a couple of entities.
code.store platform exposes the http://{your service url}/rest/{endpoint}
route, where {endpoint}
is getting routed to the file ./src/rest/{http method}.{endpoint}.ts
.
In order to create an endpoint, you need to create a new file in the src/rest
folder (by default, no such directory exists) the name of which will contain the request method (GET, POST, PUT, DELETE ...) at the beginning of the name and the name of the route. For example:
request GET http://localhost:3000/rest/helloWorld
will be routed to src/rest/get.helloWorld.ts
request POST http://localhost:3000/rest/user/create
will get routed to src/rest/post.user.create.ts
Commands: cs :generate:rest -m get -n helloWorld and cs :generate:rest -m get -n helloWorld will avoid the manual step of endpoint creation and generate files from the previous example with predefined handlers. We strongly recommend using this way of endpoint creation. More information about endpoints generation can be found in Generation - REST section.
Each newly created endpoint file must export a function that contains the business logic. Exported function takes two arguments: event and context, which contains a connection to the database and mapped request object. Each handler may release any business logic you need. You can import any library, connect to the database, execute queries... Here an example of "hello world" handler:
By running cs generate:rest -m get -n helloWorld
we will generate the following file at src/rest/get.helloWorld.ts
:
Handler function takes two arguments: event and context.
event - is an object, which provides mapped request objects. Query params
, request body
, request headers
can be accessed here. Below you can find an interface of event object:
In cases, when params
, body
and headers
are not enough to implement you can access req
and res
object. REST endpoints are based on Express framework and allow you to use all features of this framework. event object exposes request and response objects, which are Express objects and can be used in the handler as you wish.
If you use response middlewares execute next() function from event object at the end of the handler execution context.
The next POST request curl -X POST 'http://localhost:3000/rest/helloWorld?hello=world' --data '{ "test": "object" }' -H "Content-Type: application/json"
will result in the following event object:
context
- is an object, which provides TypeORM connection object. In case if database is available for your service - you can access it and establish a connection with database.
The context argument contains the database connection property which you can use with your TypeORM entities:
As you can see, the concept is very simple but is very powerful at the same time, as it allows you to create custom REST endpoints, which could be used for integration with OAuth provides, Stripe or other payment systems which require callbacks!
There is no need to use an Express response object to generate a server response, just return string
in case if string response is required or return an object, which will be sent as JSON
response, following the next interface: