使用 Express 写接口

创建基本的服务器

const express = require('express');
const cors = require('cors');


const app = express();

// 配置解析表单数据的中间件
app.use(express.urlencoded( {extended: false} ))

// 一定要在路由之前,配置 cors 这个中间件,从而解决接口跨域的问题
app.use(cors())


const router = require("./apiRouter.js")
// 把路由模块注册到 app 上
app.use("/api",router)

app.listen(80,()=>{
    console.log("http://127.0.0.1");
});

创建 API 路由模块

const express = require('express');
const router = express.Router();

router.get("/get",(req,res)=>{
    // 通过 req.query 获取客户端通过查询字符串,发送到服务器的数据
    const query = req.query;
    // 调用 res.send() 方法,向客户端响应处理的结果

    res.send({
        status: 0,
        msg: "GET 请求成功!",
        data: query,  //响应给客户端的数据
    })
})

router.post("/post",(req,res)=>{

    // 通过req.body 获取请求体中包含的 utl-encoded 格式的数据
    let body = req.body;

    res.send({
        status: 0,
        msg: "POST 请求成功!",
        data: body,  //响应给客户端的数据
    })
})

router.delete("/delete",(req,res)=>{

    res.send({
        status: 0,
        msg: "DELETE 请求成功!",
    })
})

module.exports = router;

调试接口

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <button id="btnGET">GET</button>
    <button id="btnPOST">POST</button>
    <button id="btnDelete">DELETE</button>
    <button id="btnJSONP">JSONP</button>

    <script>
        $(function(){
            // 1. 测试GET接口
            $('#btnGET').on('click', function () {
                $.ajax({
                    type: 'GET',
                    url: 'http://127.0.0.1/api/get',
                    data: { name: 'GET', age: 20 },
                    success: function (res) {
                    console.log(res)
                    },
                })
            })

            $('#btnPOST').on('click', function () {
                $.ajax({
                    type: 'POST',
                    url: 'http://127.0.0.1/api/post',
                    data: { name: 'POST', age: 20 },
                    success: function (res) {
                    console.log(res)
                    },
                })
            })

            $('#btnDelete').on('click', function () {
                $.ajax({
                    type: 'DELETE',
                    url: 'http://127.0.0.1/api/delete',
                    data: { name: 'POST', age: 20 },
                    success: function (res) {
                    console.log(res)
                    },
                })
            })
        })
    </script>
</body>
</html>
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值