项目地址
http server
题目
设计一个模拟HTTP服务端程序
自己设计一个WEB的程序,监听80端口。支持多客户端连接,能满足客户的HTTP请求(浏览器访问),包括以下功能:
1.基本功能:get、post(带数据请求)、head请求
2.模拟登陆访问,页面redirector功能(设计登陆页面login.html、主页index.html,如果直接访问index.html则跳转到登陆页面,只有登陆后才能打开主页)
3.其他(如cookie)
效果展示
思路
用户打开网址 127.0.0.1:8080
时,客户端发起 get 请求,请求路径为 /
,服务端返回 login.html 页面。
if (request.url === '/') { fs.readFile('./login.html', function (err, data) { if (!err) { response.writeHead(200, { "Content-Type": "text/html;charset=UTF-8" }); response.end(data) } else { throw err; } }); }
当用户试图通过浏览器地址访问 /index
时,服务端会判断请求头是否携带 cookie ,若没有则将请求重定向到 /
。
if (!request.headers.cookie) { response.writeHead(301, { 'Location': '/' }) response.end() }
如果有携带 cookie ,则将浏览器重定向到 index.html 页面
window.location.href = '/index'
用户在 login.html 界面输入用户名并点击登录,客户端会携带用户名发起一个 post 请求
let input = { name: document.querySelector('.input').value } let request = new XMLHttpRequest(); // 新建XMLHttpRequest对象 request.open('POST', '/login', true) request.send(JSON.stringify(input))
服务端接收参数,设置 cookie
let input = { name: document.querySelector('.input').value } let request = new XMLHttpRequest(); // 新建XMLHttpRequest对象 request.open('POST', '/login', true) request.send(JSON.stringify(input))
如果客户端发情 HEAD 请求,只返回相应头
if (request.url === '/getHead') { response.writeHead(200); response.end() }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 群星《十二女声Ⅲ》[WAV/CUE/分轨][626.4MB]
- 陈百强.1993-离不开...纪念歌集2CD【华纳】【WAV+CUE】
- 于台烟.2003-爱自己【乾坤唱片】【WAV+CUE】
- 群星.2024-我们永远是我们电影原声带【乐禾发文化】【FLAC分轨】
- 【中国民乐】张维良《秋江夜泊》2004[FLAC+CUE]
- 明达年度发烧碟MasterSuperiorAudiophile2011[DSF]
- 明达年度发烧碟MasterSuperiorAudiophile2017[DSF]
- 翁立友.2008-恨孤单【大旗】【WAV+CUE】
- 群星.2009-Magic.Cover日粤精华3CD【环球】【WAV+CUE】
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]