-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
138 lines (115 loc) · 3.99 KB
/
server.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require("dotenv").config();
const express = require("express");
const app = express();
const path = require("path");
const { logger, logEvents } = require("./middleware/logger");
const errorHandler = require("./middleware/errorHandler");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const corsOptions = require("./config/corsOptions");
const connectDB = require("./config/dbConn");
const mongoose = require("mongoose");
const PORT = process.env.PORT || 3500;
const swaggerJSDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Electronics Inventory",
version: "0.1.0",
description:
"Electronics Inventory is a full-stack website project where users can keep track of an electronics lab inventory. With thousands of small components its easy to lose track of where and how many of each part you currently have in stock. With this website you have full control over you inventory, from anywhere in the world. In order to use this website, you must have an account. This project was coded from scratch by myself as a capstone project for my career change into Software Engineering.",
license: {
name: "MIT",
url: "https://github.com/joshl26/electronics-inventory-backend/blob/master/LICENSE",
},
contact: {
name: "Joshua Lehman",
url: "https://joshlehman.ca",
email: "[email protected]",
},
},
servers: [
{
url: process.env.SERVER_URL,
},
],
},
apis: ["./routes/*.js"],
};
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
const swaggerSpec = swaggerJSDoc(options);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
mongoose.set("strictQuery", false);
const cloudinary = require("cloudinary").v2;
const Multer = require("multer");
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_KEY,
api_secret: process.env.CLOUDINARY_SECRET,
});
// console.log(cloudinary.config().cloud_name);
// console.log(cloudinary.config().api_key);
// console.log(cloudinary.config().api_secret);
const storage = new Multer.memoryStorage();
const upload = Multer({
storage,
});
async function handleUpload(file) {
const res = await cloudinary.uploader.upload(file, {
resource_type: "image",
allowedFormats: ["jpeg", "png", "jpg"],
folder: "ElectronicsInventory",
use_filename: true,
});
return res;
}
// console.log(process.env.NODE_ENV);
connectDB();
app.use(logger);
app.use(cors(corsOptions));
app.use(express.json());
app.use(cookieParser());
app.use("/", express.static(path.join(__dirname, "public")));
app.use("/", require("./routes/root"));
app.use("/auth", require("./routes/authRoutes"));
app.use("/parts", require("./routes/partRoutes"));
app.use("/notes", require("./routes/noteRoutes"));
app.use("/users", require("./routes/userRoutes"));
app.post("/parts/upload", upload.single("my_file"), async (req, res) => {
// console.log(req);
try {
const b64 = Buffer.from(req.file.buffer).toString("base64");
let dataURI = "data:" + req.file.mimetype + ";base64," + b64;
const cldRes = await handleUpload(dataURI);
res.json(cldRes);
} catch (error) {
console.log(error);
res.send({
message: error.message,
});
}
});
app.all("*", (req, res) => {
res.status(404);
if (req.accepts("html")) {
res.sendFile(path.join(__dirname, "views", "404.html"));
} else if (req.accepts("json")) {
res.json({ message: "404 Not Found" });
} else {
res.type("txt").send("404 Not Found");
}
});
app.use(errorHandler);
mongoose.connection.once("open", () => {
console.log("Connected to MongoDB");
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
});
mongoose.connection.on("error", (err) => {
console.log(err);
logEvents(
`${err.no}: ${err.code}\t${err.syscall}\t${err.hostname}`,
"mongoErrLog.log"
);
});