This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
87 lines (73 loc) · 2.31 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
require("dotenv").config();
const cron = require("node-cron");
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const mongoose = require("mongoose");
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const { fetchAndSaveTweets } = require("./fetchTweets");
const categoryController = require("./controllers/category");
const categoriesRoutes = require("./routes/categories");
const tweetsRoutes = require("./routes/tweets");
const metaRoutes = require("./routes/meta");
const verificationRoutes = require("./routes/verification");
const feedbackRoutes = require("./routes/feedback");
const app = express();
const DB_URL = process.env.MONGO_URI;
const PORT = process.env.PORT || 4000;
const swaggerDocs = swaggerJsDoc({
swaggerDefinition: {
info: {
version: "1.0.0",
title: "YellowJobs API",
description: "YellowJobs API v1",
contact: {
name: "API Support",
url: "",
},
servers: ["http://yellowjobs.org"],
},
},
apis: ["routes/*.js"],
});
app.use(morgan(process.env.NODE_ENV == "production" ? "common" : "dev"));
app.use(express.json());
app.options("/volunteer/*", cors());
app.use(cors());
app.use("/api", tweetsRoutes);
app.use("/api", categoriesRoutes);
app.use("/api", metaRoutes);
app.use("/api", verificationRoutes);
app.use("/api", feedbackRoutes);
app.use("/", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
console.log(
"⚠️Starting ",
process.env.NODE_ENV == "production" ? "prod" : "staging",
" Environment"
);
mongoose
.connect(DB_URL, {
dbName: process.env.NODE_ENV === "production" ? "prod" : "staging",
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("✅ Database Connected!");
//fetchAndSaveTweets();
if (process.env.NODE_ENV === "production") {
cron.schedule("*/5 * * * *", async () => {
console.log("Fetching Tweets...");
console.time("fetchTweets");
//await fetchAndSaveTweets();
console.timeEnd("fetchTweets");
console.log("Done Fetching Tweets!");
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log("🚀 Server Ready! at port:", PORT);
});
process.on("beforeExit", () => {
categoryController.flush();
});
});