Node.js实现一个HTTP服务器

项目地址

http serverhtml

题目

设计一个模拟HTTP服务端程序git

本身设计一个WEB的程序,监听80端口。支持多客户端链接,能知足客户的HTTP请求(浏览器访问),包括如下功能:github

1.基本功能:get、post(带数据请求)、head请求json

2.模拟登录访问,页面redirector功能(设计登录页面login.html、主页index.html,若是直接访问index.html则跳转到登录页面,只有登录后才能打开主页)浏览器

3.其余(如cookie)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
response.writeHead(200, {
  // cookie只能设置当前域名和父域名,同级域名无效
  'Set-Cookie': `name=${json.name}`,
  'Content-Type': 'text/plain',
})
response.end()
  • 若是客户端发情 HEAD 请求,只返回相应头
if (request.url === '/getHead') {
  response.writeHead(200);
  response.end()
}
相关文章
相关标签/搜索