node中get和post请求

在服务器端打开网页

// 如何在服务器端打开网页 ajax同源策略
// 1.创建服务 http
const http=require("http") //http模块
const fs=require("fs")  //fs模块
http.createServer((req,res)=>{
    // 请求 localhost:8000/index.html
    // 打开服务器端的www/index.html
    fs.readFile(`www/${req.url}`,(err,data)=>{
        if(err){
            res.write("404")
            res.end()
        }else{
            res.write(data)
            res.end()
        }
    })
}).listen(8000)

在这里插入图片描述

获取GET请求(表单)

get请求的数据放在 req.url里

// 获取get请求  通过表单
const http=require("http")
const url=require("url")
let uname="wangyi"
let upass="123"
http.createServer((req,res)=>{
// 接口地址:localhost:8000/login? uname,upass 
    let {pathname,query}=url.parse(req.url,true) //把req.url解析成对象 赋值给pathname路径,query数据
    console.log(pathname) // 获得:/login
    console.log(query) // 获得:{ uname: 'wangyi', upass: '123' }
}).listen(8000)

获取POST请求(表单)

const http = require("http")
//url querystring post querystring
const querystring=require("querystring")
http.createServer((req, res) => {
    //需要分段接受 post请求发送过来的数据是分段发送的
    
    //post请求中 获取路径  通过pathname=req.url
    console.log(req.url) // 获得:/post
    

    // 获取数据 通过以下方法
    let arr = []
    //.on绑定事件 data
    req.on("data", data => {
        //Buffer 二进制码
        arr.push(data)
    })
    //.on绑定事件 end
    req.on("end", () => {
        let buffer = Buffer.concat(arr)
        let {uname,upass} = querystring.parse(buffer.toString()) //解构{}=
        console.log(uname,upass) //获得:wangyi 123
    })
}).listen(8000)

GET和POST请求类型判定

const http=require("http")
const querystring=require("querystring")
const url=require("url")
// 由于get和post获取 参数的方式不同 所以在每次请求前 要进行判断请求类型
http.createServer((req,res)=>{
    // req.method当前请求的方法
    // path是api地址 query是获取的数据
    if(req.method=="POST"){
        let path=req.url
        let  query
            //.on绑定事件 data
        req.on("data", data => {
            //Buffer 二进制码
            arr.push(data)
        })
        //.on绑定事件 end
        req.on("end", () => {
            let buffer = Buffer.concat(arr)
            query = querystring.parse(buffer.toString()) 
        })
        if(path=="/login"){

        }
    }else if(req.method=="GET"){
        let {pathname,query}=url.parse(req.url,true)
    }
}).listen(8000)

获取GET请求(ajax)

在这里插入图片描述

// 1.创建http模块
const http=require("http")
const url=require("url")
const fs=require("fs")
// 2.登录逻辑
    // 1.用户填写用户名 密码 通过ajax发送给/login接口
    // 2.在接口对应位置 编写逻辑 判断是否存在这个用户
// 3.注册逻辑
    // 1.判断该用户是否存在,返回已经存在
let users=[{id:1,uname:"王一",upass:123}]
http.createServer((req,res)=>{
    // 解决谷歌浏览器默认请求/favicon.ico
    let { pathname, query } = url.parse(req.url, true)//把req.url解析成对象
    // console.log(query) //得到:{ uname: 'wangyi', upass: '123' }
    if(pathname=="/favicon.ico") return
    if(pathname=="/login"){
        let result=users.filter(item=>item.uname==query.uname)
        if(result.length==0){
            // 如果长度为0,证明没有该用户 将来服务器端返回回来的内容 都是Json格式
            res.write(JSON.stringify({errorCode:1001,mes:"没有该用户"}))
            res.end()
        }else{
            if(result[0].upass==query.upass){
                res.write(JSON.stringify({errorCode:200,mes:"登录成功"}))
                res.end()
            }else{
                res.write(JSON.stringify({errorCode:1001,mes:"密码错误"}))
                res.end()
            }
        }
    }else if(pathname=="/reg"){
        let result=users.filter(item=>item.uname==query.uname)
        if(result.length==0){
            let obj={
                id:users.length+1,
                uname:query.uname,
                upass:query.upass
            }
            users.push(obj)
            res.write(JSON.stringify({errorCode:200,mes:"注册成功"}))
            res.end()
        }else{
            res.write(JSON.stringify({errorCode:1001,mes:"该用户已存在"}))
            res.end()
        }
    }else if(pathname.endsWith(".html")){
        fs.readFile(`www${pathname}`,(err,data)=>{
            if(err){
                res.write(404)
                res.end()
            }else{
                res.write(data)
                res.end()
            }
        })
    }
}).listen(8000)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值