summer

Creator: coderz1093

Last updated:

0 purchases

summer Image
summer Images

Languages

Categories

Add to Cart

Description:

summer

Summer #
优雅而强大的Dart Web框架。借鉴了Express.js等开源库,支持路由管理,中间件,静态文件托管,以及各种常用的功能
简单的例子 #
import 'package:summer/summer.dart';

void main(List<String> arguments) {
var app = createApp();
app.get('/test', [
(req, res, next) {
return 'test finished';
}
]);
app.listen(httpPort: 4000);
}
copied to clipboard
app.get定义一个get请求,/test为路径,后面的回调函数负责执行业务逻辑
app.listen(httpPort: 4000),启动HTTP服务,并监听4000端口
中间件 #
import 'package:summer/summer.dart';

void main(List<String> arguments) {
var app = createApp();
var corsOptions = {'origin': 'http://localhost:4200'};
app.use(fns: [cors(corsOptions)]);
app.get('/test', [
(req, res, next) {
print('test');
}
]);
app.listen(httpPort: 4000);
}
copied to clipboard
以cors中间件为例,cors()会返回函数
使用app.use(fns: [cors(corsOptions)])就可将中间件注册
路由管理器 #
import 'package:summer/summer.dart';

void main(List<String> arguments) {
var app = createApp();
var router = httpRouter();
router.get('/test', [
(req, res, next) {
return 'test';
}
]);
app.useHttpRouter(path: '/index', router: router);
app.listen(httpPort: 4000);
}
copied to clipboard
创建路由管理器,并使用app.useHttpRouter将路由管理器注册进应用,访问接口的路径为/index/test
文件系统 #
import 'package:summer/summer.dart';

void main(List<String> arguments) {
var app = createApp();
app.use(fns: [fileDB('files')]);
app.listen(httpPort: 4000);
}
copied to clipboard
引入fileDB中间件,输入http://localhost:4000,即可访问files文件夹
静态网站 #
import 'package:summer/summer.dart';

void main(List<String> arguments) {
var app = createApp();
app.use(fns: [serveStatic('htmls')]);
app.listen(httpPort: 4000);
}
copied to clipboard
引入serveStatic中间件,输入http://localhost:4000即可访问htmls文件夹下的静态网站
文件下载 #
import 'package:summer/summer.dart';

void main(List<String> arguments) {
var app = createApp();
app.get('/download', [
(req, res, next) async {
await res.downloadFile('files/file.txt');
}
]);
app.listen(httpPort: 4000);
}
copied to clipboard
调用res.downloadFile('files/file.txt')即可
API请求 #
HTTP #
import 'package:summer/summer.dart';

var app = createApp();
app.get('/path', [(req, res, next) => next.complete(), (req, res, next) => 'test']);
app.listen(httpPort: 4000);
copied to clipboard
引入summer包后,需要调用createApp创建应用,然后可以调用app.get(http方法),输入路径以及回调函数
回调函数中的next类似于express的next,只不过summer是调用next.complete()来继续执行函数
WebSocket #
import 'package:summer/summer.dart';

void main(List<String> arguments) {
var app = createApp();
app.ws('webSocket', [
(req, ws, next) {
ws.listen((event) {
print(event);
});
}
]);
app.listen(httpPort: 4000);
}
copied to clipboard
TCP/UDP #
import 'dart:io';

import 'package:summer/summer.dart';

void main(List<String> arguments) {
var app = createApp();
app.tcp((socket, next) {
socket.listen((event) {
print(event);
});
});
app.udp((socket, next) {
socket.listen((event) {
if (event == RawSocketEvent.read) {
var data = socket.receive();
if (data != null) {
print(data.data);
}
}
});
});
app.listen(tcpPort: 4000, udpPort: 5000);
}
copied to clipboard
app.use(path: '/', fns:[...]);
copied to clipboard
app.use用来注册中间件,传递一个Handler函数数组
目前仅支持HttpHandler.path为可选值,符合该路径才调用

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.