当前位置:网站首页>Dart development server, do I have a fever?
Dart development server, do I have a fever?
2022-06-24 08:09:00 【Tangge engages in development】
Preface
recently , My team and I have developed two APP.
The client side uses Flutter, Convenient cross platform .
On the service side, the sword is on the wrong side , There is no php, pythod, java And so on. , But with Flutter Same Dart Language .
Review the whole process , Feel burned ( SAO ) It's not light , Write down this article , It's all about recording the illness . If there are other young talents , Also have Dart The idea of developing a server , There can be a reference .
Why did I think of using Dart Development server
Many developers have heard that Dart Language , It's from Flutter This client-side development framework starts with .
Use Flutter Framework to develop cross platform applications , It can ensure the consistency of all platforms to the greatest extent , And the use experience consistent with the native language , At the same time, improve work efficiency , Reduce the cost of repetitive work . be based on Dart Language , Use Flutter frame , At present, many satisfactory client applications have been developed , Major companies are also actively promoting this work .
actually ,Dart Language is not only suitable for client-side development , alike ,Dart It can also be developed as a server .
Dart The important features are as follows :
Dart Support static compilation , Comparison PHP , Pythod Other languages , Can have higher execution performance .
Dart Support asynchronous tasks , Comparison Java etc. , Born to support high concurrency .
Dart Support for object-oriented , Comparison Go etc. , Easier to model and understand .
One other thing , Need special reminders :
Dart In the field of client development , It has achieved obvious success , If it is also used in the server field Dart, You can reuse code at a higher level , Reduce communication costs , Improve development efficiency .
therefore , Use Dart Language for server-side development , It is a thing worth trying .
Write the first line of the server code
stay Dart In the server world of , At present, everything is so primitive and desolate , Even WEB Servers need to be written by themselves .
newly build main.dart file
import 'dart:io';
main() async {
var server = await HttpServer.bind(
InternetAddress.loopbackIPv4,
4040,
);
print('Listening on localhost:${server.port}');
await for (HttpRequest request in server) {
request.response
..write('Hello, world!')
..close();
}
} The above code , On the local computer 4040 port , Open the HTTP service , And receive HTTP request ,
Open the browser , visit localhost:4040 You can see the browser output Hello, world!
The code still looks simple , Not complicated .
Simple routes are used first
From the code above , You can see , HttpRequest It is generated when the browser accesses the web address , We guess he should have included the requested information .
Sure enough , open HttpRequest Source code , You can see a lot of information , such as :
- method
- uri
- headers
- cookies
- session
- connectionInfo
You can see , Are some very common WEB Concept .
among uri Next there is path , That is, the request path , in other words :
When you request in the browser \ Path time , request.uri.path The value is \
When you request in the browser \abc Path time , request.uri.path The value is \abc
When you request in the browser \admin Path time , request.uri.path The value is \admin
Then it's easy to do , if, else Walk up
import 'dart:io';
main() async {
var server = await HttpServer.bind(
InternetAddress.loopbackIPv4,
4040,
);
print('Listening on localhost:${server.port}');
await for (HttpRequest request in server) {
routeHandle(request);
}
}
void routeHandle(HttpRequest request) {
if (request.uri.path == '/abc') {
request.response
..write('Hello, abc!')
..close();
} else if (request.uri.path == '/admin') {
request.response
..write('Hello, admin!')
..close();
} else {
request.response
..write('Hello, world!')
..close();
}
}Mm-hmm , There is also a need to optimize , First look at the effect .
Simple controller to use
The controller is generally used to receive request information , Then call the system internal code to process the information , Finally, the response information is returned .
Don't talk nonsense , Get the code .
New file HomeController.dart, Type the following
import 'dart:io';
class HomeController {
static String index(HttpRequest request) {
// some other code
return 'hello world';
}
static String abc(HttpRequest request) {
// some other code
return 'hello abc';
}
static String admin(HttpRequest request) {
// some other code
return 'hello admin';
}
} stay main.dart Import the controller , And modify the content
import 'dart:io';
import 'HomeController.dart';
main() async {
var server = await HttpServer.bind(
InternetAddress.loopbackIPv4,
4040,
);
print('Listening on localhost:${server.port}');
await for (HttpRequest request in server) {
routeHandle(request);
}
}
void routeHandle(HttpRequest request) {
String content = '';
if (request.uri.path == '/abc') {
content = HomeController.abc(request);
} else if (request.uri.path == '/admin') {
content = HomeController.admin(request);
} else {
content = HomeController.index(request);
}
request.response
..write(content)
..close();
}Mm-hmm , There is also a need to optimize , later .
Simple database operations work
In the project dependency file pubspec.yaml Add new dependency mysql1: ^0.19.2
Use mysql1 A simple query
ConnectionSettings settings = new ConnectionSettings(
host: 'localhost',
port: 3306,
user: 'bob',
password: 'wibble',
db: 'mydb'
);
MySqlConnection conn = await MySqlConnection.connect(settings);
var results = await conn.query('select name, email from users where id = ?', [1]);
for (var row in results) {
print('Name: ${row[0]}, email: ${row[1]}');
});direct writing SQL, That won't lose a lot of hair , Simply package it and come back
List<Column> condition = [Column('id', '=', 1)];
List<Map<String,dynamic>> list = await Db('users').where(condition).select();
print(list);Mm-hmm , Chain operation , It's much more convenient to use .
summary
thus , We use Dart Language , Realize the request from the browser , To route , To the controller , And can operate the database .
Of course it's very simple , It needs other work to really use it .
however ( Be sure to add buts ), At least we verified Dart The feasibility of developing the server , There is another choice in the technology selection of back-end development .
What do you say ?
边栏推荐
- OC Extension 检测手机是否安装某个App(源码)
- How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
- Vulnhub target: boredhackerblog: social network
- 热赛道上的冷思考:乘数效应才是东数西算的根本要求
- Duilib display memory picture
- Pagoda panel installation php7.2 installation phalcon3.3.2
- 站在风暴中心:如何给飞奔中的腾讯更换引擎
- LINQ 查询(2)
- GraphMAE----論文快速閱讀
- redolog和binlog
猜你喜欢

某问答社区App x-zse-96签名分析

对于flex:1的详细解释,flex:1

软件工程导论——第二章——可行性研究

Graphmae ---- quick reading of papers

Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV
![Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row](/img/16/011ba3aef1315c39526daac7e3ec89.png)
Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row

Selenium IDE的安装以及使用

Moonwell Artemis is now online moonbeam network

OC Extension 检测手机是否安装某个App(源码)

On the H5 page, the Apple phone blocks the content when using fixed to locate the bottom of the tabbar
随机推荐
基于Distiller的模型压缩工具简介
Svn actual measurement common operation record operation
[test development] first knowledge of software testing
Introduction to software engineering - Chapter 2 - feasibility study
5g industrial router Gigabit high speed low delay
Shader common functions
Case examples of corpus data processing (cases related to sentence retrieval)
L1-019 who goes first (15 points)
Thread blocking
Installation and use of selenium IDE
快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning
Pipeline concept of graphic technology
OC Extension 检测手机是否安装某个App(源码)
Thread considerations
站在风暴中心:如何给飞奔中的腾讯更换引擎
第 2 篇:繪制一個窗口
Online education fades
Configure your own free Internet domain name with ngrok
Random number remarks
In the post epidemic era, the home service robot industry has just set sail