HTTP的请求与响应

HTTP请求与响应

1. 请求参数

1.1.GET参数

  • 参数被放置在浏览器地址栏中,如:Http://localhost:3000/?name=username&age=22
  • 参数获取需要借助系统模块url,url模块用来处理url地址
/****************示例代码******************/
const http = require('http');
 // 导入url系统模块 用于处理url地址
 const url = require('url');
 const server = http.createServer();
 server.on('request', (req, res) => {
     // url.parse() 第一个参数是要处理的url  第二个参数表示是否将查询参数解析成对象
    let { query, pathname } = url.parse(req.url, true);
     console.log(query);
 });
 server.listen(3000);

1.2.POST参数

  • 参数被放置在请求体中进行传输
  • 获取POST参数需要使用data事件和end事件
  • 使用querystring系统模块将参数转换为对象格式
/****************示例代码******************/
const http = require('http');
// 该模块用于将post传递的参数由字符型转化为对象
const querystring = require('querystring')

const server = http.createServer();
server.on('request', (req, res) => {
    // 用于存储post传递来的参数
    let postparams = '';
    // 监听参数开始传输事件
    req.on('data', (param) => postparams += param);
    // 监听参数传输结束事件
    req.on('end', () => console.log(querystring.parse(postparams)));

    res.end('success');
})
server.listen(3000);

2. 路由

路由指的是客户端的请求地址与服务器中逻辑代码实现的对应关系

/************************基本路由代码***********************/
// 获取系统模块
const http = require('http');
const url = require('url');
const server = http.createServer();

server.on('request', (req, res) => {
    // 获取请求方式
    let method = req.method.toLowerCase();
    // 获取请求地址
    let path = url.parse(req.url).pathname;

    res.writeHead(200, {
        'content-type': 'text/html;charset=utf-8'
    })

    if (method == 'get') {
        if (path == '/' || path == '/index') {
            res.end('欢迎来到首页');
        } else if (path == '/login') {
            res.end('欢迎来到登录页面')
        } else {
            res.end('页面不存在')
        }
    } else if (method == 'post') {
        if (path == '/' || path == '/index') {
            res.end('欢迎来到首页//通过post');
        } else if (path == 'login') {
            res.end('欢迎来到登录页面//通过post')
        } else {
            res.end('页面不存在//通过post')
        }
    }
});
server.listen(3000);
console.log('服务器启动成功');

3. 客户端请求途径

3.1. GET方式

  1. 浏览器地址栏
  2. a标签的href属性
  3. link标签的href属性
  4. script标签的src属性
  5. img标签的src属性
  6. Form表单提交

3.2. POST方式

  1. Form表单提交

4. 静态资源响应

静态资源指的是服务能够不通过任何处理就直接响应给客户端的资源,如html、css、js、image

/*****************代码示例*******************/
const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
// 引入第三方模块mime用于分析资源类型
const mime = require('mime');

const server = http.createServer();
// 当服务器接收到客户端请求时
server.on('request', (req, res) => {
    // 获取请求地址
    let pathname = url.parse(req.url).pathname;
    // 将默认地址也变为 default.html
    pathname = pathname == '/' ? '/default.html' : pathname;
    // 将用户请求路径转化为文件在服务器上的实际路径
    let realPath = path.join(__dirname, 'public' + pathname);

    // 获取资源类型
    let type = mime.getType(realPath);

    // 通过fs模块读取文件
    fs.readFile(realPath, (err, result) => {
        if (err != null) {
        	// 设置响应报文
            res.writeHead(404, {
                'content-type': 'text/html;charset=utf-8'
            })
            res.end('文件读取失败');
            return;
        }
        // 设定文件在浏览器的打开方式(响应报文)
        res.writeHead(200, {
            'content-type': type
        });
        res.end(result);
    })
});
server.listen(3000);
console.log('服务器启动成功');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值