request 获取不到
来源:6-6 API对接mysql(博客列表)
慕慕2933357
2022-08-04
app.js
const handleBlogRouter=require('./src/router/blog')
const handleUserRouter=require('./src/router/user')
//处理post data
const getPostData = (req) => {
const promise = new Promise((resolve, reject) => {
if (req.method !== 'POST') {
resolve({})
return
}
if (req.headers['content-type'] !== 'application/json') {
resolve({})
return
}
let postData = ''
req.on('data', chunk => {
postData += chunk.toString()
})
req.on('end', () => {
if (!postData) {
resolve({})
return
}
resolve(
JSON.parse(postData)
)
})
})
return promise
}
const serverHandle =(req,res)=>{
res.setHeader('Content-type','application/json')
//获取path
const url =req.url
req.path=url.split('?')[0]
console.log(req.path)
//解析query
req.query = new URLSearchParams(url.split('?')[1])
console.log(req.query)
getPostData(req).then(postData => {
req.body = postData
//处理blog路由
// const blogData=handleBlogRouter(req,res)
// if(blogData){
// res.end(
// JSON.stringify(blogData)
// )
// return
// }
const blogResult = handleBlogRouter(req.res)
if(blogResult){
blogResult.then(blogData => {
res.end(
JSON.stringify(blogData)
)
})
return
}
//处理user路由
const userData=handleUserRouter(req,res)
if(userData){
res.end(
JSON.stringify(userData)
)
return
}
// 未命中路由
res.writeHead(404,{"Content-type":"text/plain"})
res.write('404 Not Found\n')
res.end()
})
}
module.exports=serverHandle
router/blog.js
const {
getList,
getDetail,
newBlog,
updateBlog,
delBlog
} = require('../controller/blog')
const {SuccessModel,ErrorModel} = require('../model/resModel')
const handleBlogRouter = (req,res) => {
const method = req.method
const id = req.query.id
if(method === 'GET' && req.path === "/api/blog/list"){
const author = req.query.author || ''
const keyword = req.query.keyword || ''
const result = getList(author,keyword)
return result.then(listData => {
return new SuccessModel(listData)
})
}
if(method === 'GET' && req.path === "/api/blog/detail"){
const data = getDetail(id)
return new SuccessModel(data)
}
if(method === 'POST' && req.path === "/api/blog/del"){
const result = delBlog(id)
if(result){
return new SuccessModel()
}else{
return new ErrorModel('删除失败')
}
}
if(method === 'POST' && req.path === "/api/blog/new"){
const data = newBlog(req.body)
return new SuccessModel(data)
}
if(method === 'POST' && req.path === "/api/blog/update"){
const result = updateBlog(id,req.body)
if(result){
return new SuccessModel()
}else{
return new ErrorModel('更新失败')
}
}
}
module.exports = handleBlogRouter
controller/blog.js
const {exec} = require('../db/mysql')
const getList = (author,keyword) => {
let sql = `select * from blogs where 1=1 `
if(author){
sql += `and author='${author}'`
}
if(keyword){
sql += `and title like '%${author}%'`
}
sql += `order by createtime desc;`
return exec(sql)
}
const getDetail = (id) => {
return{
id:1,
title:'标题A',
content:'内容A',
createTime:1659146935340,
author:'zhangsan'
}
}
const newBlog = (blogData = {}) => {
return{
id : 3 //新建博客,插入到表中的id
}
}
const updateBlog = (id,blogData={}) => {
//id为更新博客id,blogData为博客更新后内容
return true
}
const delBlog = (id) => {
return true
}
module.exports={
getList,
getDetail,
newBlog,
updateBlog,
delBlog
}
mysql.js
const mysql = require('mysql')
const {MYSQL_CONF} = require('../conf/db')
//创建链接对象
const con = mysql.createConnection(MYSQL_CONF)
//开始链接
con.connect()
//sql操作
function exec (sql){
const promise = new Promise((resolve,reject) => {
con.query(sql,(err,result) => {
if(err){
reject(err)
return
}
resolve(result)
})
})
return promise
}
module.exports = {
exec
}
写回答
2回答
-
const blogResult = handleBlogRouter(req.res)
这一行,把 (req.res) 改成 (req, res)
012022-08-09 -
双越
2022-08-04
是 query获取不到吧。
你应该是没有对 req.query 进行赋值,检查一下代码
042022-08-05
相似问题