我正在Node.js中为https请求做一个赋值。该赋值请求使用本地https模块来修补数据。此外,我们想要更新的数据可以在js文件之后输入,比如nodexxx.js;更新某物";。问题是,当打补丁时,会上传更新,但不会更新旧数据。因此,它导致新旧数据同时存在。谢谢
const https = require('https');
const args = process.argv;
const act = args[2];
const update = args[3]
const options = {
hostname: 'reqres.in',
port: 443,
path: `/api/users/${act}`,
method: 'PATCH',
headers: {
"Content-Type": 'application/x-www-form-urlencoded'
}
}
const req = https.request(options.options_patch, res => {
let data = '';
console.log('Status: ', res.statusCode)
console.log('Headers: ', JSON.stringify(res.headers))
res.setEncoding('utf8');
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
console.log('Body: ', JSON.parse(data));
})
}).on('error', e => {
console.error(e);
})
req.write(JSON.stringify(update));
req.end();
}
const https = require('https');
const args = process.argv;
const act = args[2];
const update = args[3];
const options = {
hostname: 'reqres.in',
port: 443,
path: `/api/users/${act}`,
method: 'PATCH',
headers: {
"Content-Type": 'application/x-www-form-urlencoded'
}
}
const req = https.request(options, res => {
let data = '';
console.log('Status: ', res.statusCode);
console.log('Headers: ', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
console.log('Body: ', JSON.parse(data));
});
}).on('error', e => {
console.error(e);
});
req.write(update);
req.end();
node test.js path "data"
状态:200标题:{"日期":"星期三,2020年7月8日12:59:16 GMT","内容类型":"应用程序/json;字符集=utf-8","属性长度":"50","连接":"关闭","设置cookie":["__cfduid=d76ab451f50acf77b2fe9da83cbff44521594213156;过期时间=8月7日星期五12:59:16 GMT;路径=/;域=.reqres.in;HttpOnly;SameSite=Lax;安全"];x-powered-by":"Express"访问控制允许起源":"&"etag":"W/";32-PB4zlwTjg/gFA9Sv/cu8Rzo1N4〃&"经由":"1.1蔬菜"cf缓存状态":"DYNAMIC"cf请求id":"03d01b5e6500000c1117955200000001"期望ct":"最大年龄=604800,报告uri=";https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct""服务器":"cloudflare"cf射线":"5af9fb43d9af0c11 AMS"}正文:{data:",更新日期:"2020-07-08T12:59:16.501Z"}
FYI:这也可以用curl
或wget
来完成。