queen palace
server side dart micro-framework to handle incoming http requests.
hello world app
future<void> main(list<string> args) async {
final palace = palace();
palace.get('/greet_the_queen', (req, res) => res.send('long live the queen ��'));
await palace.opengates();
}
hello world with decoration
decoration will help you split your code to parts or modules easily making the application easy to maintain
void main(list<string> args) async {
final palace = palace();
palace.controllers([maincontroller()]);
await palace.opengates();
}
class maincontroller extends palacecontroller {
maincontroller() : super('/');
@get()
void greethequeen(request req, response res) {
return res.send('long live the queen ��');
}
}
core parts
palace
class
- register routes and the call back for each route
- use guards ‘palace.use(corsguard())’ for example
- open the server
- close the server
request
class
wrapper class around dart:io
httprequest
with extra functions and getters to make your life easier.
response
class
wrapper class around dart:io
httpresponse
with extra functions and getters also to make the same life easier .
some of these functions are
res.json(data?)
will convert the given data tojson
and sent it back to the userres.file(path)
give it path and it will give the file to the userres.notfound(data?)
=> 404res.internalservererror(data?)
=> 500res.accepted(data?)
=> 200res.created(data?)
=> 201
and so on….
middleware aka guard
��♂️
a simple function
- return
void
- takes any parameters you want starting from 0 parameters or the entire parameters list (see the parameters list down below)
- guards considered as extra layer before the handlers layers
- they can be registered for specific route or as global guard for any kind of requests
- they can response to incoming requests since they have access to the incoming request
- they can preform any kind of logic before or after the handler be triggered
palaceexception class
you can throw them from any where from your application
so guards can and handlers can or even the services can throw them
but what will happened then ?
the palace will catch the exception format it to json including the given data object – if one was provided –
and end the request life cycle
- here some of them
badrequest(data?)
notfound(data?)
unauthorized(data?)
callback parameters
if you are using the decoration you can get extra push to your endpoint callback or the guards
you can extract these type of data from the incoming request
- without decorations you can get access to the incoming request or the response by declaring the type of them
@get()
void sayhi(request req,response res) {
//logic
}
need to aces the request body directly and strong typed ?
use @body()
decorator
class signupbody{
late string name;
late string email;
late string password;
}
@post()
void signup(request req,response res,@body() signupbody body) {
//logic
}
need to access specific value from the body ?
@body('key') string email
the same goes for
@query()
@queryparam()
@param()
if you are building a guard use
@next()
to get access to the next callback
Comments are closed.