4.3.处理Post请求

app.js

const http = require('http')

const server = http.createServer((req, res) => {
    if (req.method == 'POST') {
        // 数据格式
        console.log('content-type:', req.headers['content-type'])
        // 接收数据
        let postData = ''
        req.on('data', chunk => {
            postData += chunk.toString()
        })
        req.on('end', () => {
            console.log(postData)
            res.end('hello world') // 在这里返回,因为是异步
        })
    }
})

server.listen(8000)
console.log('server is running, address is http://127.0.0.1:8000')

Last updated

Was this helpful?