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

//img.mukewang.com/szimg/60744ac209fbedeb07810439.jpg

这两个接口,

//img.mukewang.com/szimg/60744afc09a936f409250655.jpg

我这里这样写的话,postman正常,但是命令行返回的对象是空//img.mukewang.com/szimg/60744b5f09e97e4407450437.jpg

我要是这样写的话,postman一直是sending request

//img.mukewang.com/szimg/60744b9f09eaebb407990615.jpg

0
1
双越
你来 debug 一下,看代码走到了哪一行,从哪一行结束的? 不能光眼睛瞅着代码看,要动手去 debug,做起来。
2021-04-13
共1条回复

双越

2021-04-12

是哪个接口?跟踪一下代码,具体执行到了哪一行了?

----------

你得详细的描述问题,光贴一堆代码,我也不知道你问题在哪里。

0
0

Node.js+Express+Koa2+Nest.js 开发服务端

从入门到实战,一站式掌握 Node.js+Express+Koa2

4051 学习 · 2006 问题

查看课程