node搭建简易服务器
querystring和url模板学习地址 querystring&url
1. 目录结构
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);
原文地址:https://www.cnblogs.com/yyy6/p/8650188.html
时间: 2024-10-31 09:56:38