-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
212 lines (188 loc) · 5.06 KB
/
Copy pathserver.js
File metadata and controls
212 lines (188 loc) · 5.06 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const express = require("express");
const cors = require("cors");
const axios = require("axios");
const mongoose = require("mongoose");
const url = "https://api.github.com/users/karthik4423/repos";
const app = express();
app.set("view engine", "ejs");
app.use(express.static("public"));
require("dotenv").config();
//app.use(bodyParser.urlencoded({ extended: true }));
const uri = process.env.DB_URI;
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
});
const connection = mongoose.connection;
connection.once("open", () => {
console.log("MongoDB connection established successfully");
});
var repoSchema = new mongoose.Schema(
{
updated_at: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
language: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
language_url: {
type: String,
required: true,
},
},
{ _id: true, timestamps: true }
);
var languageSchema = new mongoose.Schema(
{
langdat: [],
},
{ _id: true }
);
var repoData = mongoose.model("repodata", repoSchema);
var langData = mongoose.model("langdata", languageSchema);
var reponames = [];
var languages = [];
var corsOptions = {
origin: [
"http://localhost:4200",
"http://localhost:4000",
"http://192.168.1.2:4200",
],
};
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader("Access-Control-Allow-Origin", "*");
//res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:*");
// Request methods you wish to allow
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
);
// Request headers you wish to allow
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,content-type"
);
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader("Access-Control-Allow-Credentials", true);
// Pass to next layer of middleware
next();
});
app.use(cors(corsOptions));
app.listen(80, () => {
console.log("App is listening at port 80");
});
getGitData();
setTimeout(getGitData, 36000000);
function getGitData() {
repoData
.deleteMany()
.then((result) => console.log(`Deleted ${result.deletedCount} item(s).`))
.catch((err) => console.error(`inside Delete failed with error: ${err}`));
langData
.deleteMany()
.then((result) => console.log(`Deleted ${result.deletedCount} item(s).`))
.catch((err) => console.error(`inside Delete failed with error: ${err}`));
axios
.get(url, {})
.then(function (datas) {
reponames = [];
for (var i = 0; i < datas.data.length; i++) {
if (reponames.length < 4) {
if (JSON.stringify(datas.data[i].fork) == "false") {
if (
datas.data[i].name != "karthik4423.github.io" &&
datas.data[i].name != "karthik4423"
) {
reponames.push([
datas.data[i].updated_at,
datas.data[i].name,
datas.data[i].language,
datas.data[i].description,
datas.data[i].languages_url,
]);
}
}
}
}
setTimeout(dateFormatter, 100);
})
.catch((error) => {
//console.log(error);
});
setTimeout(addtodb, 8000);
}
function dateFormatter() {
for (i = 0; i < reponames.length; i++) {
reponames[i][0] = new Date(reponames[i][0]);
}
reponames.sort(function (a, b) {
return b[0] - a[0];
});
getLanguages();
}
function getLanguages() {
for (i = 0; i < reponames.length; i++) {
axios.get(reponames[i][4], {}).then(function (datas) {
languages.push(Object.entries(datas.data));
});
}
}
function addtodb() {
for (var i = 0; i < reponames.length; i++) {
//console.log(reponames[i]);
const updated_at = reponames[i][0];
const name = reponames[i][1];
const language = reponames[i][2];
var description = reponames[i][3];
if (description == null) {
description = "dummy desc";
}
const language_url = reponames[i][4];
const landat = languages[i];
//console.log(landat);
const repo = new repoData({
updated_at,
name,
language,
description,
language_url,
});
const land = new langData({
landat,
});
//console.log(repo);
//console.log(land);
repo.save().then(() => console.log("Repodata added"));
//.catch((err) => console.log("Error : " + err));
land.save().then(() => console.log("langdata added"));
//.catch((err) => console.log("Error : " + err));
}
}
app.get("/", function (req, res) {
res.render("index");
});
app.get("/getdata", function (req, res) {
data = [];
for (var i = 0; i < reponames.length; i++) {
data.push(reponames[i], languages[i]);
}
console.log(data);
res.send(data);
});
app.get("/getlang", function (req, res) {
res.send(languages);
});
//app.get("/github", function (req, res) {});