Skip to content

Commit b787620

Browse files
committed
add exercise - saving files
1 parent c2f1ae3 commit b787620

File tree

12 files changed

+1114
-0
lines changed

12 files changed

+1114
-0
lines changed

module2/exercise_08_save_files/image_1558376232234.png renamed to module2/exercise_08_save_files/images/image_1558376232234.png

File renamed without changes.

module2/exercise_08_save_files/image_1558376240358.png renamed to module2/exercise_08_save_files/images/image_1558376240358.png

File renamed without changes.

module2/exercise_08_save_files/image_1558376266448.png renamed to module2/exercise_08_save_files/images/image_1558376266448.png

File renamed without changes.

module2/exercise_08_save_files/image_1558376281197.png renamed to module2/exercise_08_save_files/images/image_1558376281197.png

File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
constexpress=require("express");
2+
constDatastore=require("nedb");
3+
constfs=require("fs");
4+
5+
constapp=express();
6+
app.listen(3000,()=>console.log("listening at 3000"));
7+
app.use(express.static("public"));
8+
app.use("/img",express.static("images"));
9+
app.use(express.json({limit: "1mb"}));
10+
11+
constdatabase=newDatastore("database.db");
12+
database.loadDatabase();
13+
14+
app.get("/api",(request,response)=>{
15+
database.find({},(err,data)=>{
16+
if(err){
17+
response.end();
18+
return;
19+
}
20+
response.json(data);
21+
});
22+
});
23+
24+
app.post("/api",(request,response)=>{
25+
constdata=request.body;
26+
consttimestamp=Date.now();
27+
data.timestamp=timestamp;
28+
data.image_file=`image_${timestamp}.png`;
29+
constbase64Data=data.image64.replace(/^data:image\/png;base64,/,"");
30+
fs.writeFileSync(`images/${data.image_file}`,base64Data,"base64");
31+
deletedata.image64;
32+
database.insert(data);
33+
response.json(data);
34+
});

0 commit comments

Comments
(0)