I have to redefine an Express class before / deleteUser before I can make sure that the value read below is not null.
all the code is as follows
/*server.js*/
var express = require("express"); // express
var app = express();
var fs = require("fs"); //
//
var user = {
"user4": {
"name":"mohit",
"password":"password4",
"profession":"teacher",
"id":4
}
}
//
app.get("/listUsers", (req, res) => {
fs.readFile( __dirname + "/" + "users.json", "utf8", (err, data) => {
console.log( data );
res.end( data );
});
})
//
app.get("/addUser", (req, res) => {
//
fs.readFile( __dirname + "/" + "users.json", "utf8", (err, data) => {
if (err) {
return console.log(err);
};
data = JSON.parse(data); // JSON
data["user4"] = user["user4"];
console.log(data);
res.end( JSON.stringify(data) ); // JSON
});
});
//
app.get("/:id", (req, res) => {
//
fs.readFile( __dirname + "/" + "users.json", "utf8", (err, data) => {
data = JSON.parse( data );
var user = data["user" + req.params.id] // get
console.log( user );
res.end( JSON.stringify(user) );
})
})
var app = express();
//
app.get("/deleteUser", (req, res) => {
//
fs.readFile( __dirname + "/" + "users.json", "utf8", (err, data) => {
data = JSON.parse( data );
delete data["user" + 2];
console.log( data );
res.end( JSON.stringify(data));
});
})
// server
var server = app.listen(1937, () => {
console.log(server.address());
})
delete the user"s previous
var app = express();
must exist. If it is removed, it will return undefined, and cannot read data data.
Why?