-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (36 loc) · 1.04 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
import genHash from "./hashUrl.js";
import { connectDb } from "./connectDb.js";
connectDb().catch(console.log);
import { storeIndb, searchDb } from "./connectDb.js";
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
let app = express();
app.listen(3000);
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
let corsOptions = {
origin: "*",
allowedHeaders: ["Content-Type"],
};
app.post("/geturl", cors(corsOptions), async (req, res) => {
let hash = await genHash(req.body.url);
await storeIndb(hash, req.body.url);
res.json({ hash: hash });
});
app.get("/favicon.ico", (req, res) => res.status(204).end());
app.get("/:hash", async (req, res) => {
let search = await searchDb(req.params.hash);
try {
if (search.search) {
res.redirect(search.url);
res.end();
} else {
console.log("404 not found!");
res.sendFile("/Users/vinod/Desktop/URL-Shortner/public/404.html");
}
} catch (err) {
console.log("Error", err);
}
});