速过node.js

32 阅读1分钟
const http = require('http');
const url = require('url');
const qs = require('querystring');
const server = http.createServer((req,res)=>{
  const {pathname,query} =url.parse(req.url)
  if(req.method === 'GET' && pathname === '/getinfo'){
   console.log('query',query)
   console.log('query',qs.parse(query))
    res.setHeader( 'Content-Type','text/html;charset=utf-8')
    res.end( '接收到getinfo接口get的请求')
  }else if(req.method === 'POST'&& pathname === '/postinfo' ){
    let data='';
    req.on('data', temp=>{
      data+=temp
    })
    req.on('end',()=>{
      console.log('data',qs.parse(data))
      res.setHeader( 'Content-Type','text/html;charset=utf-8')
      res.end( '接收到post请求')
    })
  }
  else{
    res.statusCode =404
    res.end('Not Found')
  }
 
  
})

server.listen(3001,()=>{
  console.log("listening running")
})