前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >HTTP请求与响应处理

HTTP请求与响应处理

原创
作者头像
Qwe7
发布2022-04-22 14:11:51
发布2022-04-22 14:11:51
86900
代码可运行
举报
文章被收录于专栏:网络收集网络收集
运行总次数:0
代码可运行

(5)HTTP请求与响应处理

请求参数

客户端向服务器端发送请求时,有时需要携带一些客户信息,客户信息需要通过请求参数的形式传递到服务器端,比如登录操作。

GET请求参数

代码语言:javascript
代码运行次数:0
复制
参数被放置在浏览器地址栏中,例如:http://localhost:3000/?name=zhangsan&age=20
参数获取需要借助系统模块url,url模块用来处理url地址
 const http = require('http');
 // 导入url系统模块 用于处理url地址
 const url = require('url');
 const app = http.createServer();
 app.on('request', (req, res) => {
     // 将url路径的各个部分解析出来并返回对象
         // true 代表将参数解析为对象格式
     let {query} = url.parse(req.url, true);
     console.log(query);
 });
 app.listen(3000);

app.js

代码语言:javascript
代码运行次数:0
复制

// 用于创建网站服务器的模块
const http = require('http');
// 用于处理url地址
const url = require('url');
// app对象就是网站服务器对象
const app = http.createServer();
// 当客户端有请求来的时候
app.on('request', (req, res) => {
    // 获取请求方式
    // req.method
    // console.log(req.method);
    
    // 获取请求地址
    // req.url
    // console.log(req.url);
    
    // 获取请求报文信息
    // req.headers
    // console.log(req.headers['accept']);
    
    res.writeHead(200, {
        'content-type': 'text/html;charset=utf8'
    });

    console.log(req.url);
    // 1) 要解析的url地址
    // 2) 将查询参数解析成对象形式
    let { query, pathname } = url.parse(req.url, true);
    console.log(query.name)
    console.log(query.age)

    if (pathname == '/index' || pathname == '/') {
        res.end('<h2>欢迎来到首页</h2>');
    }else if (pathname == '/list') {
        res.end('welcome to listpage');
    }else {
        res.end('not found');
    }
    
    if (req.method == 'POST') {
        res.end('post')
    } else if (req.method == 'GET') {
        res.end('get')
    }

    // res.end('<h2>hello user</h2>');
});
// 监听端口
app.listen(3000);
console.log('网站服务器启动成功');

​```

POST请求参数

代码语言:javascript
代码运行次数:0
复制
参数被放置在请求体中进行传输
获取POST参数需要使用data事件和end事件
使用querystring系统模块将参数转换为对象格式
 // 导入系统模块querystring 用于将HTTP参数转换为对象格式
 const querystring = require('querystring');
 app.on('request', (req, res) => {
     let postData = '';
     // 监听参数传输事件
     req.on('data', (chunk) => postData += chunk;);
     // 监听参数传输完毕事件
     req.on('end', () => { 
         console.log(querystring.parse(postData)); 
     }); 
 });

post.js

代码语言:javascript
代码运行次数:0
复制

// 用于创建网站服务器的模块
const http = require('http');
// app对象就是网站服务器对象
const app = http.createServer();
// 处理请求参数模块
const querystring = require('querystring');
// 当客户端有请求来的时候
app.on('request', (req, res) => {
    // post参数是通过事件的方式接受的
    // data 当请求参数传递的时候出发data事件
    // end 当参数传递完成的时候出发end事件
    
    let postParams = '';

    req.on('data', params => {
        postParams += params;
    });

    req.on('end', () => {
        console.log(querystring.parse(postParams));
    });

    res.end('ok');

});
// 监听端口
app.listen(3000);
console.log('网站服务器启动成功');
代码语言:javascript
代码运行次数:0
复制
路由
http://localhost:3000/index
http://localhost:3000/login
路由是指客户端请求地址与服务器端程序代码的对应关系。简单的说,就是请求什么响应什么。


 // 当客户端发来请求的时候
 app.on('request', (req, res) => {
     // 获取客户端的请求路径
     let { pathname } = url.parse(req.url);
     if (pathname == '/' || pathname == '/index') {
         res.end('欢迎来到首页');
     } else if (pathname == '/list') {
         res.end('欢迎来到列表页');
     } else {
        res.end('抱歉, 您访问的页面出游了');
     }
 });
代码语言:javascript
代码运行次数:0
复制
10
// 1.引入系统模块http
// 2.创建网站服务器
// 3.为网站服务器对象添加请求事件
// 4.实现路由功能
//     1.获取客户端的请求方式
//     2.获取客户端的请求地址
const http = require('http');
const url = require('url');

const app = http.createServer();

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

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

    if (method == 'get') {

        if (pathname == '/' || pathname == '/index') {
            res.end('欢迎来到首页')
        }else if (pathname == '/list') {
            res.end('欢迎来到列表页')
        }else {
            res.end('您访问的页面不存在')
        }

    }else if (method == 'post') {

    }

});

app.listen(3000);
console.log('服务器启动成功')

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档