node学习笔记(五)网站访问量统计功能实现
2017-12-18 17:19
579 查看
index.js
count.html
index.html
const express = require('express'); const session = require('express-session'); const cookieParser = require('cookie-parser'); const querystring = require('querystring'); let app = express(); let count = 0; app.use(cookieParser("hello")); app.use(session({ secret:"hello", name:'sid', cookie:{ maxAge:10000, }, resave:true, rolling:true, })); app.get('/',function (req,res,next) { if(req.session.isLogined === 1){ res.sendfile('./count.html') }else { res.sendfile('./index.html') } }); app.post('/check',function (req,res,next) { let data = ""; req.on("data",function (chunk) { data += chunk; }); req.on("end",function () { data = querystring.parse(data); if(data.username === 'tom' && data.pwd === 'tom'){ req.session.name = data.username; req.session.isLogined = 1; res.redirect('/'); }else{ res.redirect('/'); } }); }); app.post('/form',function (req,res,next){ let fc = function(){ function foo(){ count++ ; return count; } return foo() }; res.send(`您是第${fc()}位访客`); }); app.post('/out',function (req,res){ res.clearCookie('sid') req.session.isLogined = 0; res.redirect('/') }) app.listen(8000,"¥¥¥",function () { console.log("服务挂起") });
count.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } #box{ width: 100%; height: 100px; background-color: cornflowerblue; position: absolute; bottom: 50%; transform: translateY(50%); text-align: center; font-size: 30px; line-height: 100px; } input{ top: 10%; position: absolute; left:50%; transform: translateX(-50%); width: 100px; height: 34px; background-color: cornflowerblue; color: white; border-radius: 10px; } </style> </head> <body> <div id="box"></div> <form action="http://***:8000/out" method="post"> <input type="submit" value="退出"> </form> <script> var xhr; xhr = new XMLHttpRequest(); xhr.onreadystatechange=function() { if (xhr.readyState==4 && xhr.status==200) { document.getElementById("box").innerHTML= xhr.responseText}; }; xhr.open("post","http://***:8000/form",true); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.send(null); </script> </body> </html>
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Welcome to 66</title> <style> *{ margin: 0; padding: 0; background-color: rgba(20,20,20,1); } h1{ color: white; position: relative; z-index: 2; text-align: center; line-height: 200px; } form{ color: white; font-size: 20px; font-family:"Myanmar Text"; background-color: rgba(20,20,20,1); width: 300px; height: 200px; padding: 200px; position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%); } .input{ box-shadow: 0px 0px 10px white; color: white; background-color: rgba(30,30,30,.1); width: 200px; height: 25px; border: 2px solid white; border-radius: 5px; margin: 20px 10px; } .input::placeholder{ color: rgba(255,252,252,.9); } .btn{ box-shadow: 0px 0px 30px white; width: 100px; height: 35px; position: absolute; left: 50%; transform: translateX(-50%); background-color: rgba(20,20,20,1); border: 2px solid white; color: white; border-radius: 5px; } .btn:hover{ background-color: rgba(20,20,20,1); border: 2px solid black; border-radius: 5px; box-shadow: 0px 0px 10px white; } #zhuce:hover{ background-color: rgba(20,20,20,1); border: 2px solid black; border-radius: 5px; box-shadow: 0px 0px 10px white; } #zhuce{ color: white; text-align: center; font-size: 14px; line-height: 35px; text-decoration: none; background-color: rgba(20,20,20,1); border: 2px solid white; border-radius: 5px; box-shadow: 0px 0px 30px white; width: 95px; height: 29px; position: absolute; left: 50%; bottom: 22%; transform: translateX(-50%); } small{ font-size: 15px; } </style> </head> <body> <h1>Welcome to my website!<small>账号密码为:tom</small></h1> <form action="http://***:8000/check" method="post"> <label>账号:</label> <input id="in1" class="input" type="text" name="username" placeholder="请输入用户名"> <br> <label>密码:</label> <input class="input" type="password" name="pwd" placeholder="请输入密码"> <br> <br> <input class="btn" type="submit" value="登录"> <a href="" id="zhuce">注册<a/> </form> </body> </html>
***:公网IP, ¥¥¥:私网IP,
相关文章推荐
- 学习笔记5_Day09_网站访问量统计小练习
- 使用第三方应用友盟实现网站访问量统计功能
- RIA技术下实现网站访问量统计功能
- 网站访问量统计功能的实现
- 怎样在网站中实现统计访问量的功能
- node.js学习笔记——学生信息管理的实现(把功能模块化)使用官方提供的http模块实现
- spring 3.x 学习笔记_spring mvc、spring jdbc 实现网站的登录注册功能
- Servlet学习笔记--统计网站的访问量(jsp/servlet)
- node.js学习笔记(3)-node.js结合mysql数据库实现的web项目中常见功能--登录验证、session传值、拦截器、ajax传值等
- ASP.NET学习笔记(5)--&quot;返回上一页&quot;功能的实现
- MonoRail学习笔记五:定制服务实现自定义功能
- 利用HttpSessionListener实现网站在线人数统计功能[摘]
- coolite1.0 学习笔记(二) --Combobox 获得焦点自动展开 功能实现
- 如何统计网站访问量,代码实现使用全局变量 Application,start,end,Session_start,Session_end事件
- ITCAST视频-Spring学习笔记(使用JDK中的Proxy技术实现AOP功能)
- servlet学习笔记5——分页实现(登录网站的完善)
- 关于java打印功能的最简单实现的学习笔记
- MonoRail学习笔记五:定制服务实现自定义功能
- 利用HttpSessionListener实现网站在线人数统计功能
- 利用HttpSessionListener实现网站在线人数统计功能