forked from nahidhasan141400/me.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (101 loc) · 2.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require('express');
const app = express();
const { v4 : uuidv4 } = require('uuid') ;
const cookieParser = require('cookie-parser');
const ejs = require('ejs');
const path = require('path');
const fs = require("fs");
require('dotenv').config();
// configar multer
const multer = require("multer");
let storage = multer.diskStorage({
destination:(req,file,cb)=>{
cb(null,"myData/")
},
filename:(req,file,cb)=>{
const unicName = `${file.originalname}`;
cb(null,unicName);
}
});
let upload = multer({
storage,
limits:{ fileSize: 1000000*100},
fileFilter:(req,file,cb)=>{
if(file.mimetype === "application/json"){
cb(null,true)
}else{
cb(new Error("only json file are allow to upalod"))
}
}
}).single("file");
// set port
const PORT = process.env.PORT || 3300;
// config cors
const cors = require("cors");
app.use(cors({
origin : "*"
}))
// uuid
// uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
// console.log(uuidv4());
app.use(express.static('public'));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cookieParser());
// set Template engine
app.set('views', path.join(__dirname, '/views'))
app.set('view engine', 'ejs');
const dataPath = path.join(__dirname+'/data/data.json');
const cookPath = path.join(__dirname+'/data/cook.json');
const myDataPath = path.join(__dirname+'/data/mydata.json');
// route
const { auth } = require('./medelware/auth');
//upload my data.
app.post("/upload",auth,(req,res)=>{
// if(!req.file){
// return res.json({err:"upload file"})
// }
upload(req,res,(err)=>{
if(err){
return res.status(500).send({error: err.message})
}else{
let sdata = Read(myDataPath);
if(sdata.indexOf(req.file.filename) !== -1){
return res.send('file name already exist!')
}
sdata.push(req.file.filename);
Write(myDataPath,sdata)
res.redirect('/api')
}
})
});
app.get('/mydatadelete/:name',auth,(req,res)=>{
let items = Read(myDataPath);
let {name} = req.params;
let index = items.indexOf(name)
let numberOfElementToRemove = 1;
if (index !== -1) {
items.splice(index,numberOfElementToRemove);
Write(myDataPath,items);
fs.unlinkSync( path.join(__dirname,path.normalize(`./myData/${name}`)));
res.redirect('/api')
} else{
return res.send({error:"something is wrong!"})
}
});
// route
const rout = require('./router/router');
const { Read, Write } = require('./lib/crud');
rout(app);
app.use((err,req,res,next)=>{
if(err){
res.status(500).send(err.message);
}else{
res.send("sucsses")
}
})
// init server
app.listen(PORT , () => {
console.log(`server start on:`)
console.log(`http://localhost:${PORT}`)
})