//解析session (使用redis)
let needSetCookie = false
let userId = req.cookie.userid
if(!userId){
needSetCookie = true
userId =`${Date.now()}_${Math.random()}`
//初始化 session
set(userId,{})
}
req.sessionId = userId
get( req.sessionId).then(sessionData=>{
if(sessionData ==null){
//初始化redis 中session的值
set(req.sessionId,{})
//设置session
req.session ={}
}else{
//设置session
req.session = sessionData
}
console.log('req.session',req.session)
//处理post data
return getPostData(req)
}).then(postData => {
console.log(postData)
req.body = postData
//处理blog路由
const blogResult = handleBlogRouter(req, res)
if (blogResult) {
blogResult.then(blogData => {
if (needSetCookie) {
console.log(needSetCookie)
res.setHeader('Set-Cookie', `userid=${userId};path=/; httpOnly; expires=${getCookieExpires}`)
}
res.end(
JSON.stringify(blogData)
)
})
return
}
const userResult = handleUserRouter(req, res)
if (userResult) {
userResult.then(userData => {
if (needSetCookie) {
res.setHeader('Set-Cookie', `userid=${userId};path=/; httpOnly; expires=${getCookieExpires}`)
}
res.end(
JSON.stringify(userData)
)
})
return
}