明知 | TypeScript 结合 egg.js 基本使用
小小又进入了学习状态,此时小小由于最近接触了js的相关内容,进而接触了一些ts相关的内容,所以小小本次主要学习的内容是ts。
安装相关依赖
这里安装两个依赖,分别为egg和ts
安装ts
这里需要确保首先安装了npm相关工具。
全局安装ts
npm install -g typescript
进行全局的测试
$ tsc -v Version 3.2.2
这样就完成了本地全局的ts的安装
安装egg
这里实现全局安装egg,并初始化依赖项目。
创建工作目录
mkdir showcase && cd showcase
安装相关的依赖
npm init egg --type=ts
安装依赖
npm i
运行项目
npm run dev
出现以下提示,即代表已经启动,并安装完成
C:\Users\Administrator\Desktop\untitled4555\ming>npm run dev > ming@1.0.0 dev C:\Users\Administrator\Desktop\untitled4555\ming > egg-bin dev [egg-ts-helper] create typings\app\controller\index.d.ts (5ms) [egg-ts-helper] create typings\config\index.d.ts (16ms) [egg-ts-helper] create typings\config\plugin.d.ts (10ms) [egg-ts-helper] create typings\app\service\index.d.ts (5ms) [egg-ts-helper] create typings\app\index.d.ts (2ms) 2020-07-31 14:27:49,701 INFO 12416 [master] node version v13.11.0 2020-07-31 14:27:49,703 INFO 12416 [master] egg version 2.27.0 2020-07-31 14:27:59,512 INFO 12416 [master] agent_worker#1:28076 started (9799ms) 2020-07-31 14:28:10,469 INFO 12416 [master] egg started on http://127.0.0.1:7001 (20 765ms)
访问页面效果如上
编写控制器
添加相对应的类的方法
public async show() { const { ctx } = this; // eslint-disable-next-line @typescript-eslint/no-unused-vars const page = ctx.query.page; console.log(page); const result = 'ming'; console.log(result); await ctx.render('ming.tpl', result); }
添加相关路由
import { Application } from 'egg'; export default (app: Application) => { const { controller, router } = app; router.get('/', controller.home.index); router.get('/ming', controller.home.show); };
添加模板渲染插件
编辑默认配置文件
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg'; export default (appInfo: EggAppInfo) => { const config = {} as PowerPartial<EggAppConfig>; // override config from framework / plugin // use for cookie sign key, should change to your own and keep security config.keys = appInfo.name + '_1596175919808_6331'; // add your egg config in here config.middleware = []; // add your special config in here const bizConfig = { sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`, }; config.view = { defaultViewEngine: 'nunjucks', mapping: { '.tpl': 'nunjucks', }, }; // the return config will combines to EggAppConfig return { ...config, ...bizConfig, }; };
添加相关插件
import { EggPlugin } from 'egg'; const plugin: EggPlugin = { nunjucks: { enable: true, package: 'egg-view-nunjucks', }, }; export default plugin;
访问链接
http://127.0.0.1:7001/ming
服务层编写
这里配置相关的服务层。
定义相关接口
export interface NewsItem { id: number; title: string; }
编写相关的控制器
// 定义相关方法 // eslint-disable-next-line @typescript-eslint/no-unused-vars public async list(name: number): Promise<NewsItem[]>{ name = name; return [{id:3, title: "ming"}] ; }
在控制层中调用
public async show() { const { ctx } = this; // eslint-disable-next-line @typescript-eslint/no-unused-vars const page = ctx.query.page; console.log(page); const result = 'ming'; console.log(result); await ctx.render('ming.tpl', result); }
调用显示结果
此时完成了最基本的服务层的搭建
中间件
中间件一般用于jwt验证相关的内容。这里使用jwt做前后端的验证。
创建新的中间件目录
import { Context, Application, EggAppConfig } from "egg"; export default function uuidMiddleWare(options: EggAppConfig['uuid'], app: Application): any { return async (ctx: Context, next: () => Promise<any>) => { // name 就是 config.default.js 中 uuid 下的属性 ctx = ctx; console.info(options.name); await next(); }; }
创建相关的配置文件用于中间件读取相关的内容
config.default.js
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg'; export default (appInfo: EggAppInfo) => { const config = {} as PowerPartial<EggAppConfig>; // override config from framework / plugin // use for cookie sign key, should change to your own and keep security config.keys = appInfo.name + '_1596175919808_6331'; // add your egg config in here config.middleware = ['uuid']; // add your special config in here const bizConfig = { sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`, local: { msg: 'local', }, uuid: { name: 'ebuuid', maxAge: 1000 * 60 * 60 * 24 * 365 * 10, }, }; config.view = { defaultViewEngine: 'nunjucks', mapping: { '.tpl': 'nunjucks', }, }; // the return config will combines to EggAppConfig return { ...config, ...bizConfig, }; };
读取效果如下

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
从微服务到 Serverless | 开源只是开始,终态远没有到来
开源只是开始,终态远没有到来。 从微服务开源,到 Serverless 开源,我们正持续将阿里沉淀的技术能力进行开放。 只是这一次不同的地方在于,借助 Serverless 能力的释放,将帮助前端进入一个崭新的时代,只需写几个函数即可实现后端业务逻辑,推动业务快速上线,让整个前端研发效能大幅提升。 什么是 Mindway Serverless 自7月初发布 V1.0 以来,Midway Serverless 已经获得了超过 2.7k 的 star。Midway Serverless 是一套面向 Serverless 的解决方案,包括框架、运行时、工具链、配置规范4个部分,这4部分组合之后,就能提供了一些面向 Serverless 体系的特有能力: 1、平台间迁移更容易 通过提供统一的配置规范以及入口抹平机制,让代码在每个平台基本相同; 扩展不同云平台的运行时 API,不仅能加载通用的平台间扩展,也能接入公司内部的私有化部署方案。 2、让应用更易维护和扩展 提供了标准的云平台函数出入参事件定义; 提供了多套和社区前端 React、Vue 等融合一体化开发的方案; 使用了 TypeScri...
- 下一篇
入门 | egg.js 入门之egg-jwt
小小继续学习,这次学习的内容是egg-jwt 相关。 创建egg项目 这里创建一个egg新项目,这里使用的是ts模式。 npm init egg --type=ts npm install 安装相关的包 这里创建并安装完成以后,需要再次初始化俩包,分别为egg-cors与egg-jwt token 生成的验证包 npm install egg-cors egg-jwt --save 配置相关插件 这里配置相关的插件 import { EggPlugin } from 'egg'; const plugin: EggPlugin = { jwt: { enable: true, package: "egg-jwt" }, cors: { enable: true, package: 'egg-cors', } }; export default plugin; 配置默认配置文件 config.jwt = { secret: "123456"//自定义 token 的加密条件字符串 }; config.security = { csrf: { enable: false, ignoreJS...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS关闭SELinux安全模块
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS7设置SWAP分区,小内存服务器的救世主