const http = require('http'); // 先删除所有旧数据 const req1 = http.request({ hostname: 'localhost', port: 3000, path: '/api/finance/delete-all', method: 'POST', headers: { 'Content-Type': 'application/json' } }, res => { let b = ''; res.on('data', c => b += c); res.on('end', () => { console.log('Deleted all:', b); // 添加新的收支数据 const payments = [ { type: 'income', party: '张三', amount: 1500, method: '微信', category: '租金收入', remark: '租车费' }, { type: 'income', party: '李四', amount: 2000, method: '支付宝', category: '租金收入', remark: '租车费' }, { type: 'income', party: '王五', amount: 500, method: '现金', category: '押金退还', remark: '还车退押金' }, { type: 'expense', party: '赵六', amount: 300, method: '微信', category: '退款', remark: '提前还车退款' }, { type: 'expense', party: '房东', amount: 1500, method: '银行卡', category: '房租', remark: '门店房租' }, { type: 'expense', party: '电工', amount: 200, method: '现金', category: '水电', remark: '门店电费' } ]; let i = 0; const addPayment = () => { if (i >= payments.length) { console.log('Done!'); return; } const data = JSON.stringify(payments[i]); const req = http.request({ hostname: 'localhost', port: 3000, path: '/api/finance', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length } }, res => { let b = ''; res.on('data', c => b += c); res.on('end', () => { console.log('Added:', payments[i].type, payments[i].party, payments[i].amount); i++; addPayment(); }); }); req.on('error', e => console.error(e)); req.write(data); req.end(); }; addPayment(); }); }); req1.on('error', e => console.error(e)); req1.end();