Im trying to create a PUT method to update a obj.json file and display its contents after its been uploaded. I get an empty object and no contents?
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files){
response.writeHead(200, {'content-type': 'application/json'});
response.write('received upload:\n\n');
response.end(util.inspect({fields: fields, files: files}));
});
form.on('file', function(field, file) {
console.log(file);
if (file.type == 'application/json') {
jsonObject = JSON.parse(fs.readFileSync(file.path, 'utf8'));
console.log(jsonObject);
} else { console.log('wrong file');}
});
}
function update(response, request) {
console.log("Request handler 'update' was called.");
response.writeHead(200, {"Content-Type": "application/json"});
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files){
response.writeHead(200, {'content-type': 'application/json'});
response.write('ready to update:\n\n');
response.end(util.inspect({fields: fields, files: files}));
});
form.on('file', function(field, file) {
console.log(file);
if (file.type == 'application/json') {
JSON.parse(fs.writeFileSync(file.path, JSON.stringify('"test":"Okay"'), 'utf8'));
console.log(jsonObject);
} else { console.log('wrong file');}
});
}
I was able to upload the file and access the json but I can't change the contents of the file now. Any thoughts on how to do that? Also, would delete method be the same?