Components
As Dartive is structure after Express.js, it has the same components as Express.js to ensure that users of ExpressJS would have a familiar syntax, one which is hard typed to deal with, and smoothen the transition from ExpressJS to Dartive.
Main Function
The main function contains the code that will be executed when the program is run. The main function is the entry point of a Dart program. main() is the first function that will be called in every Dart program. The main() function returns void and has an optional List<String>
parameter for arguments.
void main();
What it does on a high level is to create a new instance of the Dartive class and call the listen method on it.
Components
Dartive.post('/test', (Dartive api) async {
var body = api.request;
return body;
});
The highlighted code illustrates the components which are required to create a valid Dartive Function.
To create the required end point, you start with Dartive.
followed by the type of HTTP response that you would like the server to recieve,
along with the path that you would like to use. The path can be a string or a regular expression.
The second component is the function that will be executed when the server recieves a request. This function must return a value, which will be sent to the client as a response. In most cases, you can use the Dartive object to access the request and response objects. The request object contains information about the request, such as the headers and body. The response object contains methods for sending a response to the client.
Dartive Class
The Dartive class is the main class of the Dartive library. It is used to create a new Dartive server. The Dartive class has the following methods:
Dartive.listen()
The listen() method is used to start the server. It takes two optional parameters: host and port. The host parameter specifies the hostname or IP address of the server. The default value is localhost. The port parameter specifies the port number of the server. The default value is 8080.
Dartive.listen(host: '0.0.0.0', port: 8080);
Dartive Object
The Dartive object is used to access the request and response objects. The Dartive object has the following properties:
Dartive.request
The request property is used to access the request object. The request object contains information about the request, such as the headers and body.
isPost()
The isPost() method is used to check if the request method is POST.
isGet()
The isGet() method is used to check if the request method is GET.
isPut()
The isPut() method is used to check if the request method is PUT.
isDelete()
The isDelete() method is used to check if the request method is DELETE.
isMap()
The isMap() method is used to check if the request method is MAP.
request
The request property is used to access the request object. The request object contains information about the request, such as the headers and body.
res
The response property is used to access the response object. The response object contains methods for sending a response to the client.
request.json()
Everyone's favourite function to parse JSON data from the request body. You can use this to parse JSON data from the request body. However, for more complicated parses, you can use a model class that you share with the client(usually Flutter) to parse the data.
Dartive.get()
The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
import 'package:dartive/dartive.dart';
void main(List<String> arguments) {
Dartive.get('/', () {
return {'message': 'Hello, world!'};
});
Dartive.listen(host: '0.0.0.0', port: 8080);
}
Dartive.post()
The POST method is used to send data to the server to create a new resource. The data is included in the body of the request. This may be a resource of any type that can be sent in a request body, including a file.
import 'package:dartive/dartive.dart';
void main(List<String> arguments) {
Dartive.post('/user', (request) {
var data = request.body;
String name = data['name'];
return {'message': 'User $name created successfully!'};
});
Dartive.listen(host: '0.0.0.0', port: 8080);
}
Dartive.put()
The PUT method replaces all current representations of the target resource with the request payload.
import 'package:dartive/dartive.dart';
void main(List<String> arguments) {
Dartive.put('/user/:id', (request) {
var data = request.body;
String name = data['name'];
String id = request.params['id'];
return {'message': 'User $name updated successfully!'};
});
Dartive.listen(host: '0.0.0.0', port: 8080);
}
Dartive.delete()
The DELETE method deletes the specified resource.
import 'package:dartive/dartive.dart';
void main(List<String> arguments) {
Dartive.delete('/user/:id', (request) {
String id = request.params['id'];
return {'message': 'User $id deleted successfully!'};
});
Dartive.listen(host: '0.0.0.0', port: 8080);
}