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
写回答

1回答

双越

2021-04-13

这个问题重复了吧?

0
0

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

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

4051 学习 · 2006 问题

查看课程