• 简单node服务器demo,麻雀虽小,五脏俱全


    //本服务器要实现的功能如下:
    //1.静态资源服务器(能读取静态资源)
    //2.能接收get请求,并能处理参数
    //3.能接收post请求,并能处理参数
    
    const http = require('http');
    const fs = require('fs');
    const url = require('url');
    const querystring = require('querystring');
    
    const server = http.createServer();
    server.on('request', (req, res) => {
        //用于存放get / post数据 
        let getParams = '',postParams = '';
    
        //处理get
        const obj = url.parse(req.url, true);
        let pathname = obj.pathname;
        getParams = obj.query;
    
        console.log('你发送的get数据如下:',getParams)
        //处理post
        let str = '';
        req.on('data',(data)=>{
            str += data;
        })
        req.on('end',()=>{
            postParams = querystring.parse(str);//将字符串转换为对象
            console.log('你发送的post数据如下:',postParams)
        })
        //处理文件
        if(pathname === '/'){
            pathname = '/index.html'
        }
        if(pathname.indexOf('favicon') != -1){
            return
        }
        let fileName = './' + pathname;
        fs.readFile(fileName,(err,data)=>{
            if(err){
                console.log(pathname)
                console.log(err)
            }else{
                res.write(data)
            }
            res.end();
            
        })
    
    })
    
    server.listen(8080, () => {
        console.log('服务器开启成功!')
    });
    //本服务器要实现的功能如下:
    //1.静态资源服务器(能读取静态资源)
    //2.能接收get请求,并能处理参数
    //3.能接收post请求,并能处理参数

    const http = require('http');
    const fs = require('fs');
    const url = require('url');
    const querystring = require('querystring');

    const server = http.createServer();
    server.on('request', (reqres=> {
        //用于存放get / post数据 
        let getParams = '',postParams = '';

        //处理get
        const obj = url.parse(req.urltrue);
        let pathname = obj.pathname;
        getParams = obj.query;

        console.log('你发送的get数据如下:',getParams)
        //处理post
        let str = '';
        req.on('data',(data)=>{
            str += data;
        })
        req.on('end',()=>{
            postParams = querystring.parse(str);//将字符串转换为对象
            console.log('你发送的post数据如下:',postParams)
        })
        //处理文件
        if(pathname === '/'){
            pathname = '/index.html'
        }
        if(pathname.indexOf('favicon'!= -1){
            return
        }
        let fileName = './' + pathname;
        fs.readFile(fileName,(err,data)=>{
            if(err){
                console.log(pathname)
                console.log(err)
            }else{
                res.write(data)
            }
            res.end();
            
        })

    })

    server.listen(8080, () => {
        console.log('服务器开启成功!')
    });
  • 相关阅读:
    怎么用一句话把一个集合的属性值根据条件改了,而其他值不变
    【算法导论学习笔记】第3章:函数的增长
    【算法导论学习笔记】第2章:算法基础
    【算法导论学习笔记】第1章:算法在计算中的作用
    【Python爬虫学习笔记(3)】Beautiful Soup库相关知识点总结
    【Python爬虫学习笔记(2)】正则表达式(re模块)相关知识点总结
    【Python爬虫学习笔记(1)】urllib2库相关知识点总结
    string类型转换JSONObjec类型,并取出对应的值
    mybatis的Example的使用
    github上传项目
  • 原文地址:https://www.cnblogs.com/lguow/p/11806333.html
Copyright © 2020-2023  润新知