postman 一直sending requset
来源:5-10 开发路由(新建和更新博客路由)
weixin_慕桂英823842
2021-04-12
app.js
const handleBlogRouter = require('./src/router/blog')
const handleUserRouter = require('./src/router/user')
const queryString = require('querystring')
const getPostData = (req) => {
return new Promise((resolve, reject) => {
if (req.method !== "POST") {
resolve({});
return;
}
// if (req.headers["content-type"] !== "application/json") {
// resolve({});
// return;
// }
let postData = '';
req.on('data', chuck => {
postData += chuck.toString();
});
req.on('end', () => {
if (!postData) {
console.log(req.method);
resolve({});
return;
}
JSON.parse(postData);
})
});
}
const serverHandle = (req,res) => {
//设置返回格式是json
res.setHeader('Content-type', 'application/json')
const url = req.url
req.path = url.split('?')[0]
req.query = queryString.parse(url.split('?')[1])
//处理postData
getPostData(req).then(postData => {
req.body = postData;
//处理blog
const blogData = handleBlogRouter(req, res);
if (blogData) {
res.end(JSON.stringify(blogData));
return;
}
const userData = handleUserRouter(req, res);
if (userData) {
res.end(JSON.stringify(userData));
return;
}
//未命中 返回404
res.writeHead(404, {"Content-type": "text/plain"})
res.write('404 NOT FOUND\n')
res.end()
})
}
module.exports = serverHandle```
const handleBlogRouter = require('./src/router/blog')
const handleUserRouter = require('./src/router/user')
const queryString = require('querystring')
const getPostData = (req) => {
return new Promise((resolve, reject) => {
if (req.method !== "POST") {
resolve({});
return;
}
// if (req.headers["content-type"] !== "application/json") {
// resolve({});
// return;
// }
let postData = '';
req.on('data', chuck => {
postData += chuck.toString();
});
req.on('end', () => {
if (!postData) {
console.log(req.method);
resolve({});
return;
}
JSON.parse(postData);
})
});
}
const serverHandle = (req,res) => {
//设置返回格式是json
res.setHeader('Content-type', 'application/json')
const url = req.url
req.path = url.split('?')[0]
req.query = queryString.parse(url.split('?')[1])
//处理postData
getPostData(req).then(postData => {
req.body = postData;
//处理blog
const blogData = handleBlogRouter(req, res);
if (blogData) {
res.end(JSON.stringify(blogData));
return;
}
const userData = handleUserRouter(req, res);
if (userData) {
res.end(JSON.stringify(userData));
return;
}
//未命中 返回404
res.writeHead(404, {"Content-type": "text/plain"})
res.write('404 NOT FOUND\n')
res.end()
})
}
module.exports = serverHandle
const {getList, getDetail, newBlog} = require('../controller/blog')
const {SuccessModel, ErrorModel} = require('../model/resModel')
const handleBlogRouter = (req, res) => {
const method = req.method
if (method === 'GET' && req.path === '/api/blog/list') {
const author = req.query.author || ''
const keyword = req.query.keyword || ''
const listData = getList(author, keyword)
return new SuccessModel(listData)
}
if (method === 'GET' && req.path === '/api/blog/detail') {
const id = req.query.id;
return getDetail(id);
}
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') {
return {
meg: '更新列表'
}
}
if (method === 'POST' && req.path === '/api/blog/del') {
return {
meg: '删除博客'
}
}
}
module.exports = handleBlogRouter
写回答
2回答
-
weixin_慕桂英823842
提问者
2021-04-12
这两个接口,
我这里这样写的话,postman正常,但是命令行返回的对象是空
我要是这样写的话,postman一直是sending request
012021-04-13 -
双越
2021-04-12
是哪个接口?跟踪一下代码,具体执行到了哪一行了?
----------
你得详细的描述问题,光贴一堆代码,我也不知道你问题在哪里。
00
相似问题