Develop Server
Simple sample code has been included in the farrow-vite-react
project template, now let's unpack the server-side part.
Look at the server/api/greet.ts file first
- import farrow-api to define an api
- import farrow-api-server to contain a set of api for a service router
- import farrow-schema to define the schema structure of api
Then, define the input schema of the greet api by ObjectType. the simplest way is demonstrated here, without the description of the fields
And then define the output schema of the greet api by ObjectType. Here a structure with a description field is used, and the type of the field needs to be configured at the [Type]
property.
Define an api function with input schema and output schema, Api({ description, input, output }, fn)
,implement this api in function fn
.
greet
is a normal function that is not yet associated with any server
. We can package multiple api
's into entries
using the ApiService
function of the farrow-api-server
module.
ApiServer(options)
return a router
object of farrow-http
which can be mount on the specified route.
Then look at the server/api/index.ts
file。
It imports the farrow-http
Router
, imports the greet service
defined earlier, then creates the Router
and hooks the greet service
to the path /api/greet
.
In the server/api/index.ts
module, we can compose each api service
as needed and expose a separate router
object to the outside.
And then, in server/index.ts
, create the http
and mount it on the router
, then listen(port)
to start an http server
.
In server/index.ts
, we have enabled services
and vite
, which handle api
and page
respectively.
Next, let's take a look at the front-end development. Executing the command npm run dev
will start the application.