-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
69 lines (59 loc) · 1.79 KB
/
app.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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const multer = require('multer');
const cors = require("cors");
require("dotenv").config();
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.ykdgy.gcp.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`;
const learnRoute = require("./Routers/learnRoute");
const authRoute = require("./Routers/authRoute");
const populateData = require("./utils/startPopulate");
const Word = require("./Models/word");
const app = express();
const store = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'Images');
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString + "-" + file.originalname);
}
});
const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/jpg' || file.mimetype === 'image/png' || file.mimetype === 'image/jpeg') {
cb(null, true);
} else {
cb(null, false);
}
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(multer({storage: store, fileFilter: fileFilter}).single('image'));
app.use("/users", authRoute);
app.use("/", learnRoute);
//Database connection and server listener
mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then((connect) => {
//return Word.find().remove(); //empty words collection
return Word.countDocuments();
})
.then((count) => {
if (count < 120) {
return populateData.populateDB();
} else {
return "Already populated!";
}
})
.then((success) => {
app.listen(8000, () => {
console.log("Running in 8000!");
});
})
.catch((err) => {
console.log("Error connecting to database!");
});