// js搭建接口服务器
// 1.导入 express
const express = require('express');
// 2.创建web服务器
const app = express();
// 2.1 实现get请求
app.get('/user', (req, resp) => {
// 调用express提供的res.send()方法,向客户端响应一个JSON对象
resp.send({name: 'zs', age: 20, gender: '男'});
});
// 2.2 接收参数
app.get('/api', (req, resp) => {
// 获取参数
pwd = req.query.pwd;
pwd_res = btoa(pwd);
resp.send({'pwd': pwd_res});
});
// 2.3 动态传参 (rest风格)
app.get('/dp/:id/:name', (req, resp) => {
// req.params 是动态匹配到的URL参数,默认也是一个空对象
console.log(req.params);
resp.send(req.params);
});
// 3.启动web服务器
app.listen(8080, () => {
console.log('express server running at http://127.0.0.1:8080');
});
js搭建接口服务器
最新推荐文章于 2024-01-05 11:30:00 发布