egg+vscode调试

按F1,稍等片刻,会出现下图,在红框中输入Toggle Auto Attach开启调试模式
image.png
然后命令行启动

egg-bin debug --inpsect-brk=9229

如果调试模式没有开启成功,按F1,稍等片刻,会出现如下图,在红框中输入Attach to Node Process
image.png
然后再稍等片刻,会出现下图,随便选一个就行,如果没有的话先在命令行执行以下代码
image.png
完成上述步骤就会在左下角出现下图的标记
image.png
启动成功后此处会变成橙色
image.png
右上角会有下图标志
image.png

转载:https://segmentfault.com/a/1190000021259583

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Egg.js 是一个基于 Node.js 和 Koa 的 Web 框架,它提供了很多便捷的功能和插件,让开发者可以快速地构建 Web 应用程序。TypeScript 则是一种静态类型检查的编程语言,可以帮助我们在编写代码时更早地捕获一些错误,提高代码的可维护性和可读性。MySQL 则是一种常用的关系型数据库,用于存储和管理数据。 在 Egg.js 中使用 TypeScript 和 MySQL,我们可以使用 egg-mysql 插件来连接和操作 MySQL 数据库,并使用 TypeScript 的类型检查来保证代码的正确性。具体步骤如下: 1. 创建 Egg.js 项目 ```bash $ npm i egg-init -g $ egg-init egg-typescript-mysql --type=ts $ cd egg-typescript-mysql $ npm i ``` 2. 安装 egg-mysql 插件 ```bash $ npm i egg-mysql ``` 3. 配置 MySQL 数据库连接 在 `config/config.default.ts` 中添加以下配置: ```typescript config.mysql = { client: { host: 'localhost', port: '3306', user: 'root', password: 'password', database: 'test', }, app: true, agent: false, }; ``` 其中,`host`、`port`、`user`、`password`、`database` 分别为你的 MySQL 数据库连接信息。 4. 创建 Model 在 `app/model` 目录下创建一个新的文件 `user.ts`,用于定义操作用户数据的 Model: ```typescript import { Application } from 'egg'; export default (app: Application) => { const { INTEGER, STRING } = app.Sequelize; const User = app.model.define('user', { id: { type: INTEGER, primaryKey: true, autoIncrement: true, }, name: { type: STRING(50), allowNull: false, }, age: { type: INTEGER, allowNull: false, }, }); return User; }; ``` 其中,`app.Sequelize` 是通过 egg-sequelize 插件提供的 Sequelize 实例。 5. 创建 Controller 和 Service 在 `app/controller` 目录下创建一个新的文件 `user.ts`,用于处理用户相关的请求: ```typescript import { Controller } from 'egg'; export default class UserController extends Controller { async index() { const { ctx } = this; const users = await ctx.service.user.list(); ctx.body = { users }; } async show() { const { ctx } = this; const user = await ctx.service.user.get(ctx.params.id); ctx.body = { user }; } async create() { const { ctx } = this; const { name, age } = ctx.request.body; const user = await ctx.service.user.create(name, age); ctx.body = { user }; } async update() { const { ctx } = this; const { id, name, age } = ctx.request.body; const user = await ctx.service.user.update(id, name, age); ctx.body = { user }; } async destroy() { const { ctx } = this; const user = await ctx.service.user.delete(ctx.params.id); ctx.body = { user }; } } ``` 在 `app/service` 目录下创建一个新的文件 `user.ts`,用于实现对用户数据的操作: ```typescript import { Service } from 'egg'; export default class UserService extends Service { async list() { const { ctx } = this; const users = await ctx.model.User.findAll(); return users; } async get(id: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); return user; } async create(name: string, age: number) { const { ctx } = this; const user = await ctx.model.User.create({ name, age }); return user; } async update(id: number, name: string, age: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); if (!user) { ctx.throw(404, 'user not found'); } await user.update({ name, age }); return user; } async delete(id: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); if (!user) { ctx.throw(404, 'user not found'); } await user.destroy(); return user; } } ``` 6. 路由配置 在 `app/router.ts` 中配置路由: ```typescript import { Application } from 'egg'; export default (app: Application) => { const { controller, router } = app; router.get('/users', controller.user.index); router.get('/users/:id', controller.user.show); router.post('/users', controller.user.create); router.put('/users', controller.user.update); router.delete('/users/:id', controller.user.destroy); }; ``` 7. 启动应用 ```bash $ npm run dev ``` 以上就是使用 Egg.js、TypeScript 和 MySQL 开发 Web 应用程序的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值