as mentioned, I now submit a json file from the front end and want to parse the json through the back-end node.js. How can I read all the parameters in it?
json file:
{
"person": {
"name": "wanger",
"birth": "1999"
}
}
Code:
router.post("/upload", function(req, res){
var form = new formidable.IncomingForm();
form.uploadDir = path.join(__dirname, "/upload");
//
form.on("file", function(field, file) {
fs.rename(file.path, path.join(form.uploadDir, file.name));
});
fs.readFile("test.json", function(err, data) {
if (err)
throw err;
var obj = JSON.parse(data.toString());
console.log("the result: " + obj);
//console.log(data.toString());
});
});
there are two main problems,
1. How can I use readFile to read req files directly instead of parsing them locally?
2. I use json.parse, but every time I want to display obj, I always show: objec Object, instead of specific content. How to read birth and name of person?