node搭建简易服务器

  querystring和url模板学习地址 querystring&url

  1. 目录结构

  NODE简易综合应用服务器搭建

  2. 代码结构

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

let server = http.createServer((req,res) =>{
    // 获取get请求数据
    let obj = url.parse(req.url,true);
    let urlBli = obj.pathname;
    let GET = obj.query;

    // 获取post请求数据
    let str = '';
    let POST = null;
    req.on('data',(data) => {
        str += data;
    });
    req.on('end', () => {
        POST = querystring.parse(str);
    });

    // 请求资源文件
    let filename = './www/'+ urlBli;
    fs.readFile(filename,(err,data) => {
        if(err){
            res.write('404,该页面不存在');
        }else {
            res.write(data);
        }
        res.end();
    })
});

server.listen(7676);

 

相关文章:

  • 2022-01-22
  • 2021-12-03
  • 2021-07-13
  • 2021-04-22
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-11-15
猜你喜欢
  • 2022-01-08
  • 2021-08-05
  • 2021-08-20
  • 2022-12-23
  • 2022-01-01
  • 2021-12-16
相关资源
相似解决方案