赞
踩
编写REST API,实际上就是编写处理HTTP请求的async函数,不过,REST请求和普通的HTTP请求有几个特殊的地方:
Content-Type
为application/json
;Content-Type
也是application/json
。package.json:项目描叙
- {
- "name": "rest-koa",
- "version": "1.0.0",
- "description": "rest-koa project",
- "main": "app.js",
- "scripts": {
- "dev": "node --use_strict app.js"
- },
- "keywords": [
- "koa",
- "rest",
- "api"
- ],
- "author": "david pan",
- "dependencies": {
- "koa": "2.0.0",
- "koa-bodyparser": "3.2.0",
- "koa-router": "7.0.0"
- }
- }
app.js
-
- const Koa = require('koa');
- const app = new Koa();
-
- const bodyParser = require('koa-bodyparser');
- const controller = require('./controller');
- const rest = require('./rest');
-
- // parse
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。