-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
120 lines (108 loc) · 2.89 KB
/
Copy pathapp.js
File metadata and controls
120 lines (108 loc) · 2.89 KB
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
import express from "express";
import feedRoutes from "./routes/feed.js";
import authRoutes from "./routes/auth.js";
import { connectDb } from "./util/db.js";
import path from "path";
import multer from "multer";
import fs from "fs";
import { Server } from "socket.io";
// import { init } from "./socket.js";
import { graphqlHTTP } from "express-graphql";
import { schema } from "./graphql/schema.js";
import { root } from "./graphql/resolvers.js";
import { error } from "console";
import { isAuth } from "./middleware/auth.js";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
app.use("/images", express.static(path.join(process.cwd(), "images")));
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
cb(null, Date.now() + "-" + file.originalname);
},
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg" ||
file.mimetype === "image/png"
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.use(express.json());
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).single("image")
);
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
if (req.method === "OPTIONS") {
return res.sendStatus(200);
}
next();
});
app.use(isAuth);
app.put("/post-image", (req, res, next) => {
if (!isAuth) {
throw new Error(" Not authenticated!");
}
if (!req.file) {
return res.status(200).json({ message: "No file provided!" });
}
if (req.body.oldPath) {
clearImage(req.body.oldPath);
}
return res
.status(201)
.json({ message: "File stored", filePath: req.file.path });
});
app.use(
"/graphql",
graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
formatError(err) {
if (!err.originalError) {
return err;
}
const data = err.originalError.data;
const message = err.message || "An error occured";
const code = err.originalError.code || 500;
return {
message: message,
status: code,
data: data,
};
},
})
);
try {
await connectDb();
} catch (err) {
console.error("Failed to connect database:", err);
}
// app.use("/feed", feedRoutes);
// app.use("/auth", authRoutes);
app.listen(8080);
// const io = init(server);
// io.on("connection", (socket) => {
// console.log("socket connected");
// });
export const clearImage = (filePath) => {
filePath = path.join(__dirname, "..", filePath);
fs.unlink(filePath, (err) => console.log(err));
};