安装
npm i -g @adonisjs/cli
通过项目模板初始化项目
# Fullstack(全栈)
> git clone --dissociate https://github.com/adonisjs/adonis-fullstack-app
# API(接口)
> git clone --dissociate https://github.com/adonisjs/adonis-api-app
# Slim(简化版)
> git clone --dissociate https://github.com/adonisjs/adonis-slim-app
安装依赖
npm install
启动服务
adonis serve --dev
常用 adonis 命令
# 创建控制器
adonis make:controller Admin/User --type http
解析json参数
安装依赖BodyParser
cnpm i --save @adonisjs/bodyparser
修改配置
start/app.js
const providers = [
'@adonisjs/bodyparser/providers/BodyParserProvider'
]
start/kernel.js
const globalMiddleware = [
'Adonis/Middleware/BodyParser'
]
允许跨域
cnpm i --save @adonisjs/cors
修改配置文件
start/app.js
const providers = [
'@adonisjs/cors/providers/CorsProvider'
]
start/kernel.js
Server
.use(['Adonis/Middleware/Cors'])
新增配置文件config/cors.js
'use strict'
module.exports = {
origin: true,
methods: ['GET', 'PUT', 'PATCH', 'POST', 'DELETE'],
headers: true,
exposeHeaders: false,
credentials: false,
maxAge: 90
}