Request flow in Lesan
We only accept two types of HTTP
methods in Lesan:
- GET
- POST
GET Requests:
We accept two models of GET
requests:
-
Requests sent to serve a
static
document.
In order to receive astatic
document with aGET
request, you must first allow itspath
to Lesan, for this you only need to add the following settings when calling therunServer
function:coreApp.runServer({ staticPath: ["/pictures", "/videos"] });
The
staticPath
key receives anarray
ofstrings
Now all the files in the
pictures
orvideos
folder will be accessible by sending aGET
request, for example:https://www.xyz.com/pictures/avatar.png
-
Requests sent to receive
playground static
documents or access to theplayground
itself.
In order to access theplayground
, you must set theplayground
entry in therunServer
function equal totrue
.coreApp.runServer({ playground: true });
The
playground
key receives aboolean
value.Now you can access the
playground
by sending aGET
request to an address similar tohttp://localhost:1367/playground
.
Note that the following addresses will be accessible together with theplayground
and will send the necessarydata
to run theplayground
:- /playground/static/index.css
which sends the necessarystyles
for theUI
in theplayground
. - /playground/static/bundle.js
which sends aJS bundle
of all the codes needed to run theplayground
. - /playground/static/get/schemas
which sends twoJSON
data,schemas
andacts
, which contain all the information we need in theplayground
to sendrequests
to theserver
.
- /playground/static/index.css
POST Requests:
We accept two models of POST
requests:
- Requests sent to
receive data
from theserver
(in fact, these requests are the main requests sent to Lesan). - Requests sent to
upload
a document.
Receive data
To receive data
from the Lesan server, you must send a POST
request to the final address of Lesan.
In the body
of this request, must be an object
in JSON
format with the following keys:
- The
service
key is used to select one of themicroservices
set on the application. This key is optional and by default the value ofmain
is placed in it. - The
model
key is used to select one of theModels
added to the application. - The
act
key is used to select one of theActs
added to the application. - The
details
key is used to receive data to be sent from the client side along with data to be delivered to users. This key has two internal keys calledget
andset
, we talked a little about it before.set
: It contains the information we need in theAct
function.get
: Contains selected information that the user needs to be returned. This selection is based onzero
orone
. Therefore, we can pass this object directly to MongoDBprojection
.
An example of this JSON
object is as follows:
{
"service": "main",
"model": "country",
"act": "addCountry",
"details": {
"set": {
"name": "Iran",
"population": 85000000,
"abb": "IR"
},
"get": {
"_id": 1,
"name": 1,
"population": 1,
"cities": {
"name": 1,
"population": 1,
}
}
}
}
This request finally reaches the function we specified for Act
to extract the necessary information from it and return the information requested by the user.
If you remember, we set up each Act
as follows:
coreApp.acts.setAct({
schema: "user",
actName: "addUser",
validator: addUserValidator(),
fn: addUser,
preValidation: [setUser, checkLevel],
preAct: [justAdmin],
validationRunType: "create",
});
Upload documents
For uploading a document you should send a POST
request to Lesan endpoint.