HTTP 协议 get post 请求方式

 

// 创建服务器模块
const http = require('http');
// app 对象就是网站服务器对象 
const app = http.createServer();
// 当有请求的时候 
app.on('request', (req, res) => {
    // 获取请求方式
    // req.method
    // console.log(req.method);

    // 获取请求地址
    // console.log(req.url);

    // 获取请求报文信息
    // req.headers
    // console.log(req.headers['accept']);


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

    if (req.url == '/index' || req.url == '/') {
        res.end('<h2>welcome to homepage,欢迎来到首页</h2>');
    } else if (req.url == '/list') {
        res.end('welcome 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 222 user</h2>');
});
// 监听端口
app.listen(3000);
console.log('网站服务器启动成功...');

原文地址:https://www.cnblogs.com/ericblog1992/p/13084747.html