服务器http请求(get/post)

服务器选用的是nodejs,语言使用typescript;
在线json验证:http://www.json.cn/
JSON.parse(string) :将JSON型字符串转换成一个 jsonobject 对象。
JSON.stringify(obj) : 将jsonobject 对象转换为一个 JSON型字符串。
需要装环境:
安装就不进行赘述了,学习基本原理,文章1:https://blog.csdn.net/iningwei/article/details/89927228
简单实现,文章2:https://blog.csdn.net/iningwei/article/details/100105487
使用vscode开发代码部分。结合终端命令进行安装库的操作。
然后需要一个掉接口的测试工具;http://coolaf.com/
下载一个本地的工具,解压之后运行会自动打卡http://localhost:9090/tool/post的界面,即接口测试页面。测试的时候启动的黑框界面不要关。多动手试试就会用了。
在这里插入图片描述
在这里插入图片描述
重要的代码部分,只是简单测试:
get请求的格式形如是:127.0.0.1:8889/login?account=20200101&passwoard=123456

import * as http from "http";

// 使用命令行启动服务器,只需要开终端在index.js的文件夹用node index.js即可  ctrl+c取消
// ------------------------------------------------------
//get请求
// let server: http.Server;
// server = http.createServer(function (request, response) {
//     response.writeHead(200, { "Content-Type": "text/plain" });
//     let type = request.url?.split('?')[0];
//     let data = request.url?.split('?')[1];
//     let account;
//     let password;
//     if (type == "/login") {
//         account = data?.split('&')[0].split('=')[1];
//         password = data?.split('&')[1].split('=')[1];
//         console.log(account);
//         console.log(password);
//     }
//     response.write("account:" + account + "passward:" + password);
//     console.log("account:" + account + "passward" + password);
//     response.end();
// }).listen(8889);
// -------------------------------------------
//post请求
let server: http.Server;
server = http.createServer(function (request, response) {
    let msg="";
    request.on('data',function(chunk){
        msg+=chunk;
    });
    request.on('end',function(){
        if(msg!=null){
            let obj=JSON.parse(msg);
            let obj1={
                "acount":obj.data.acount,
                "age":"22",
                "sex":"woman",
            }
            obj.data=obj1;
            response.end(JSON.stringify(obj));
        }
    });
}).listen(8889);

学习网站:菜鸟教程
终端命令:
切换到项目根目录
tsc
//将ts编译为js
node ./dist/index.js
//启动服务器

express

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.get('/msg', (req, res) => {
    res.send({ massage: 'Hello World Express' });
});
app.listen(3002, () => {
    console.log('Server is running on port 3002');
});

hyper-express

const HyperExpress = require('hyper-express');
const cors = require('cors');
const app = new HyperExpress.Server();
app.use(cors());
app.get('/msg', (req, res) => {
    res.json({ massage: 'Hello World with HyperExpress' });
});
app.listen(3001, () => {
    console.log('Server is running on port 3001');
});
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值