-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·128 lines (102 loc) · 3.24 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
"use strict";
// Module Dependencies.
const express = require("express");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const errorHandler = require("errorhandler");
const dotEnv = require("dotenv");
const path = require("path");
const expressValidator = require("express-validator");
const expressStatusMonitor = require("express-status-monitor");
const sass = require("node-sass-middleware");
const favicon = require("serve-favicon");
const robots = require("express-robots");
const CronJob = require("cron").CronJob;
//const jsonWebToken = require("./middleware/jsonwebtoken");
require("express-group-routes");
// Load environment variables from .env file, where API keys and passwords are configured.
dotEnv.load({ path: ".env" });
// Create Express server.
const app = express();
// Controllers (route handlers).
const database = require("./controllers/databaseController");
const youtube = require("./controllers/youtubeController");
const cron = require("./controllers/cronController");
// Use morgan to log requests to the console
app.use(morgan("dev"));
// Express configuration.
app.set("host", process.env.OPENSHIFT_NODEJS_IP || "0.0.0.0");
app.set("port", process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.use(expressStatusMonitor());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.use(
sass({
src: path.join(__dirname, "public"),
dest: path.join(__dirname, "public")
})
);
app.use(express.static(path.join(__dirname, "public")));
app.use(favicon(path.join(__dirname, "public", "favicon.ico")));
app.use(robots(path.join(__dirname, "public", "robots.txt")));
app.disable("etag");
//app.use("/api", jsonWebToken.verifyJsonWebToken);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
// Index Route
app.get("/", (req, res) => {
res.status(200).send("welcome to this API");
});
// Version 1 API
app.group("/api/v1", router => {
// Youtube Search API
router.get("/search", youtube.requestSearchData);
// Youtube Playlist API
router.get("/playlist", youtube.requestPlaylistData);
// Youtube Trending API
router.get("/trending", youtube.requestTrendingData);
router.get("/stream", youtube.requestStreamData);
// Youtube Related Video Search
router.get("/suggest", youtube.requestRelatedData);
// Youtube Audio Download
router.get("/download", youtube.requestRelatedData);
});
// Call Mysql Connection Object
//database.mysqlConnect();
// Call Sequelize Connection
database.sequelizeConnection();
// Error Handler.
app.use(errorHandler());
// Start Express server.
app.listen(app.get("port"), () => {
console.log("Server Start");
});
// If Production then Execute
if (process.env.APP_ENV.toUpperCase() == "PROD") {
app.all("*", function(req, res) {
res.redirect(process.env.PRODUCTION_URL);
});
} else {
app.all("*", function(req, res) {
res.redirect(process.env.DEVELOPMENT_URL);
});
}
new CronJob(
"* * * 1 * *",
function() {
// Execute code here
cron.logicCronScheduling();
},
null,
true,
"Asia/Kolkata"
);
// Export
module.exports = app;