AdonisJs的快速起步

路由: routes

Route.get('/hello', ({ request }) => {
  return `hello ~ ${ request.input('name') }`
})

控制器:Controllers

adonis make:controller Hello
create app/Controllers/Http/HelloController.js
http://127.0.0.1:3333/hello?name=boloog

视图:Views

adonis make:view hello
✔ create resources/views/hello.edge

数据库:Database

adonis install sqlite3

添加数据表

adonis make:migration Post
✔ create database/migrations/1531930044179_post_schema.js

  this.create('posts', (table) => {
    table.increments()
    table.string('title')
    table.text('content', 'longtext')
    table.timestamps()
    })
  • 运行应用的migration
  • adonis migration:run
migrate: 1503248427885_user.js
migrate: 1503248427886_token.js
migrate: 1531930044179_post_schema.js
Database migrated successfully in 111 ms

引入数据库:插入与查询数据,添加路由查看全部内容

const Database = use('Database')
// 查看数据表里的所有数据
Route.get('/posts', async () => {
  return await Database.table('posts').select('*')
})

adonis repl //打开命令交互模式
> await use('Database').table('posts').insert({ title: 'apple', content: '苹果'})
> await use('Database').table('posts').insert({ title: 'orange', content: '橘子'})

http://127.0.0.1:3333/posts

你可能感兴趣的:(AdonisJs的快速起步)