DartVM服务器开发(第十天)--Jaguar环境搭建
2018-08-30 本文已影响37人
rhyme_lph
上一篇文章我们讲解了如何搭建aqueduct环境如何搭建,哈哈!其实搭建不成功,啥都没讲,不知道怎么回事,我使用win系统跟mac系统都是报同样的错误!今天我们来一起搭建jaguar环境吧!
1.创建jaguar环境项目
我们打开WebStrom,点击new Project ,然后选中dart,在右边输入自己的项目名字,点击Create

按照之前的步骤在项目里新建一个
pubspec.yaml
文件,然后输入name
、descript
、version
、environment
对应的值
我们来讲一下
environment
是什么吧,它是在dart2之后需要添加的,说明当前环境在一个大概的版本,如果你使用dart2的外部包,就需要添加,不然就会报错,我的环境添加了jaguar的依赖,说明使用jaguar这个框架,最后,我们在下面的终端中输入pub get
获取依赖包
可以看到,我们成功的添加了依赖,然后我们按照之前的添加main.dart、bin文件夹,lilb文件夹,static文件夹

然后我们在main.dart文件中添加下面代码
import 'package:jaguar/jaguar.dart';
main()=>new Jaguar()
..staticFile('/', 'static/index.html')
..staticFiles('/static/*', 'static')
..log.onRecord.listen(print)
..serve(logRequests: true);
这里解释一下
new Jaguar()
可传入
- address
- 监听的ip地址,默认为localhost(如果手机可访问,请监听自己网络ip4或ip6的ip地址)
- port
- 端口号,默认为8080
- multiThread
- 是否端口有多个服务
- securityContext
- 使用https支持
- autoCompress
- 是否自动压缩响应
- basePath
- 默认为空,添加后所有路径都要加这个前缀
- errorWriter
- 当请求发生错误,添加错误页
- sessionManager
- 自定义会话管理
..staticFile('/', 'static/index.html') 根目录返回一个index.html文件
..staticFiles('/static/*', 'static') 当请求路径为/static/*
返回根目录下的 static
目录下的文件
..log.onRecord.listen(print) 监听print
打印的信息
..serve(logRequests: true); 启动服务器,logRequests
为true
时打印请求信息
好了,我们启动一下服务器,然后请求一下地址http://localhost:8080


主页html

ok我们的环境基本上搭建完了,可以看到,使用jaguar框架还是比较爽的,接收请求的方式是链式路由,非常方便!今天的环境搭建就到这里了!我们明天见!
如果想继续学习DartVM服务器开发,请关注我,学习更多骚操作!