-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (46 loc) · 1.56 KB
/
server.js
File metadata and controls
54 lines (46 loc) · 1.56 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const githubRouter = require("./routes/Github");
const app = express();
const axios = require("axios");
const { Readable } = require("stream");
require("dotenv").config();
const PORT = process.env.PORT || 8070;
app.use(cors());
app.use(bodyParser.json());
const URL = process.env.MONGODB_URL;
mongoose.connect(URL);
const connection = mongoose.connection;
connection.once("open", () => {
console.log("Mongodb Connection Success!");
});
app.listen(PORT, () => {
console.log(`Server is up and running on port ${PORT}`);
});
app.use("/github", githubRouter);
// give message about backend working status
app.get("/", async (req, res) => {
const url =
"https://img.shields.io/badge/System%20status-Working-success?style=for-the-badge";
sendingSvg(url, res);
});
// give error search term not found
app.get("/:anything", async (req, res) => {
const search = req.params.anything;
const url = `https://img.shields.io/badge/Error-${search}%20not%20fount-red?style=for-the-badge`;
sendingSvg(url, res);
});
async function sendingSvg(url, res) {
try {
const svgResponse = await axios.get(url, { responseType: "stream" });
res.set("Content-Type", "image/svg+xml");
res.set("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate");
const svgStream = Readable.from(svgResponse.data);
svgStream.pipe(res);
} catch (error) {
console.error(error);
res.status(500).send("Error retrieving SVG");
}
}