创建node post接口请求 并返回Json
req 请求对象,想知道req有哪些属性,可以查看 “http.request 属性整合”。
res 响应对象 ,收到请求后要做出的响应。想知道res有哪些属性,可以查看 “http.response属性整合”。
const http = require('http');
const server = http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/api/data') {
let data = '';
req.on('data', (chunk) => {
data += chunk;
});
req.on('end', () => {
try {
const json = JSON.parse(data);
// 在这里可以对接收到的数据进行处理
const response = {
success: true