|
@@ -6,13 +6,22 @@ const data = "./data.json";
|
|
|
|
|
|
|
|
app.use(express.json());
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
|
|
+// Add Access Control Allow Origin headers
|
|
|
|
|
+app.use((req, res, next) => {
|
|
|
|
|
+ res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
+ res.header(
|
|
|
|
|
+ "Access-Control-Allow-Headers",
|
|
|
|
|
+ "Origin, X-Requested-With, Content-Type, Accept",
|
|
|
|
|
+ );
|
|
|
|
|
+ next();
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
app.get("/api/text", (req, res) => {
|
|
app.get("/api/text", (req, res) => {
|
|
|
fs.readFile("data.json", "utf8", (err, data) => {
|
|
fs.readFile("data.json", "utf8", (err, data) => {
|
|
|
if (err) {
|
|
if (err) {
|
|
|
res.send(err);
|
|
res.send(err);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- res.set("Access-Control-Allow-Origin", "*");
|
|
|
|
|
res.send(data);
|
|
res.send(data);
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
@@ -47,8 +56,7 @@ app.post("/api/add", (req, res) => {
|
|
|
|
|
|
|
|
fs.writeFileSync("data.json", arrjson);
|
|
fs.writeFileSync("data.json", arrjson);
|
|
|
|
|
|
|
|
- res.set("Access-Control-Allow-Origin", "*");
|
|
|
|
|
- //res.send(text);
|
|
|
|
|
|
|
+ res.send(text);
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -82,8 +90,7 @@ app.post("/api/remove", (req, res) => {
|
|
|
fs.writeFileSync("data.json", arrjson);
|
|
fs.writeFileSync("data.json", arrjson);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- res.set("Access-Control-Allow-Origin", "*");
|
|
|
|
|
- //res.send("Removed element at id: " + id);
|
|
|
|
|
|
|
+ res.send("Removed element at id: " + id);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
app.listen(port, () => {
|