转载请注明出处:
Eggjs 是 Node.js 服务端应用开发框架,它提供了一套约定,使开发者能够快速搭建、开发和部署应用。以下是 Egg.js 的一些特性和作用:
首先需要保证本地具有nodej的环境,使用egg初始化项目
npm init egg --type=simple
在执行该命令的中间过程,需要输入 项目名称、项目描述、项目作者以及安全的配置key
创建之后的项目导入vscode之后如下图所示:
1 app/router.js:用于配置URL路由规则;
2 app/controller/** :用于解析用户的输入,处理后返回相应的结果;
3 app/service/: 用于编写业务逻辑层;
4 app/public/: 用于放置静态资源;
5 config/config.{env}.js: 用于编写配置文件;
6 config/plugin.js 用于配置需要加载的插件;
egg提供5种配置文件:
config.default.js:默认配置文件;
config.local.js:开发环境下的配置,与默认配置合并,同名则覆盖默认配置;
config.prod.js:生产环境下的配置,与默认配置合并,同名则覆盖默认配置;
config.test.js:测试环境下的配置,与默认配置合并,同名则覆盖默认配置;
config.unittest.js:单元测试环境下的配置,与默认配置合并,同名则覆盖默认配置;
在 config.default.js 中添加influxdb相关的配置文件,并设置日志级别
config.influxdb = { host: 'influx_db_host', port: 8086, database: 'influx_db', username: 'influx_user', password: 'influx_pwd' } config.log = { level: 'DEBUG', consoleLevel: 'DEBUG', }
在app/model/user.js中创建user表:
module.exports = app => { const { InfluxDB } = require('influx'); const { host, port, database, username, password } = app.config.influxdb; const influx = new InfluxDB({ host, port, database, username, password, schema: [ { measurement: 'user', fields: { name: InfluxDB.FieldType.STRING, age: InfluxDB.FieldType.INTEGER, }, tags: [ 'id' ] } ] }); class User extends app.Service { async create(user) { return influx.writePoints([ { measurement: 'user', tags: { id: user.id }, fields: { name: user.name, age: user.age } } ]); } async find(id) { return influx.query(`select * from user where id='${id}'`); } async update(id, user) { return influx.writePoints([ { measurement: 'user', tags: { id }, fields: { name: user.name, age: user.age } } ]); } async delete(id) { return influx.query(`delete from user where id='${id}'`); } } return User; };
在app/controller/home.js中编写增删改查的接口:
const Controller = require('egg').Controller; class HomeController extends Controller { async create() { const { ctx } = this; const { id, name, age } = ctx.request.body; await ctx.service.user.create({ id, name, age }); ctx.body = { success: true }; } async find() { const { ctx } = this; const { id } = ctx.query; const result = await ctx.service.user.find(id); ctx.body = result; } async update() { const { ctx } = this; const { id } = ctx.params; const { name, age } = ctx.request.body; await ctx.service.user.update(id, { name, age }); ctx.body = { success: true }; } async delete() { const { ctx } = this; const { id } = ctx.params; await ctx.service.user.delete(id); ctx.body = { success: true }; } } module.exports = HomeController;
module.exports = app => { const { router, controller } = app; router.post('/user', controller.home.create); router.get('/user', controller.home.find); router.put('/user/:id', controller.home.update); router.delete('/user/:id', controller.home.delete); };
启动
npm run dev
npm start
使用curl命令测试接口:
# 创建user $ curl -X POST -H "Content-type: application/json" -d '{"id": "1", "name": "Alice", "age": 20}' http://localhost:7001/user # 查找user $ curl http://localhost:7001/user?id=1 # 更新user $ curl -X PUT -H "Content-type: application/json" -d '{"name": "Bob", "age": 30}' http://localhost:7001/user/1 # 删除user $ curl -X DELETE http://localhost:7001/user/1
egg-influxdb-demo ├── app │ ├── controller │ │ └── home.js │ ├── model │ │ └── user.js │ ├── router.js │ ├── service │ │ └── user.js │ └── view │ └── home.tpl ├── config │ ├── config.default.js │ └── plugin.js ├── package.json └── README.md