crud-client

Created at 2016-10-10 Updated at 2016-10-10 Category 学习 Tag 后台

crud (客户端)从客户端操作数据库

客户端通过不同的request来实现对数据库的crud

request包括verb 和 path

verb {get(获取),post(写内容),put(更新),delete(删除)}

1
2
3
4
5
6
7
8
9
10
get/posts //读取全部文章
post/posts //新建一篇文章
pull/posts/:id //更新一篇文章
get/posts/:id //读取一篇文章
delete/posts/:id //删除一篇文章

这就是所谓的rest架构

#express 路由控制 + mongoose

  1. 实现express路由

    express路由跑在服务器上,响应客户端发出的request决定那部分后台代码要被执行(任务分发)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    var express = require('express');
    var app = express()
    app.get('/posts',function(req,res){
    console.log('get posts')
    })
    app.post('/posts',function(req,res){
    console.log('post')
    })
    app.put('/posts/:id',function(req,res){
    console.log('put')
    })
    app.get('/posts/:id',function(req,res){
    console.log('get/posts/:id')
    })
    app.delete('/posts/:id',function(req,res){
    console.log('delete')
    })
    app.listen(3000,function(){
    console.log('running on post 3000')
    })

    2.curl工具模拟调试

    $ curl –request PUT localhost:3000/posts

    $ curl –request GET localhost:3000/posts

    $ curl –request GET localhost:3000/posts/sds

    $ curl –request PULL localhost:3000/posts/sds

    $ curl –request DELETE localhost:3000/posts/sds

API

什么是API ?

API是由当前程序提供出来的,提供给另外一个程序的开发者使用的程序接口,对于前端开发者来说就是WEB API

Site by Mickey using Hexo & Random

Hide