-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (55 loc) · 1.56 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
// Require the framework and instantiate it
const fastify = require("fastify")({ logger: false });
const { MongoClient } = require("mongodb");
const dotenv = require("dotenv");
dotenv.config();
const MONGODB_URL = process.env.MONGODB_URL;
const PORT = process.env.PORT || 5000;
fastify.register(require("fastify-cors"), {});
fastify.register(require("fastify-rate-limit"), {
global: true,
max: 3,
timeWindow: 1000 * 60,
errorResponseBuilder: function (req, context) {
return {
code: 429,
error: "Too Many Requests",
message: `I only allow ${context.max} requests per ${context.after} to this API. Try again after ${context.ttl} ms !`,
};
},
});
const client = new MongoClient(MONGODB_URL);
// Declare a route
fastify.get("/", async (req, res) => {
const db = client.db("jwoc");
const collection = db.collection("mentors");
const selectedMentors = await collection
.find({ isSelected: true })
.project({
name: 1,
email: 1,
github: 1,
linkedIn: 1,
projectName: 1,
projectLink: 1,
projectTags: 1,
projectDescription: 1,
_id: 0,
})
.toArray();
res.send(selectedMentors);
});
// Connect the DB and Run the server!
client
.connect()
.then(() => startServer())
.catch((error) => console.log(error.message));
const startServer = async () => {
try {
await fastify.listen(PORT);
console.info(`Server is listening on PORT: ${PORT} ...`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};