-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (83 loc) · 2.34 KB
/
index.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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const app = express();
const mongoose = require("mongoose");
const dns = require("dns");
// Basic Configuration
const port = process.env.PORT || 3000;
const Schema = mongoose.Schema;
app.use(cors());
app.use("/public", express.static(`${process.cwd()}/public`));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const urlSchema = new Schema({
original_url: { type: String, required: true },
short_url: { type: Number, required: true },
});
const Urls = mongoose.model("Urls", urlSchema);
app.get("/", (req, res) => {
res.sendFile(process.cwd() + "/views/index.html");
});
// Your first API endpoint
app.get("/api/hello", (req, res) => {
res.json({ greeting: "hello API" });
});
app.get("/api/shorturl/:short_url", async (req, res) => {
const urlParam = req.params.short_url;
try {
const url = await Urls.findOne({ short_url: urlParam });
// if exist
if (url) return res.redirect(url.original_url);
// if does not exist
return res.json({
error: "No short URL found for the given input",
});
} catch (err) {
console.log(err);
}
});
app.post("/api/shorturl", async (req, res) => {
const urlString = req.body.url;
// check if valid url
try {
const parsedUrl = new URL(urlString);
// check if url already in database
const url = await Urls.findOne({ original_url: urlString });
// if url already in database
if (url)
return res.json({
original_url: url.original_url,
short_url: url.short_url,
});
// check if valid hostname
dns.lookup(parsedUrl.host, async (error, address) => {
if (error) {
res.json({
error: "Invalid URL",
});
} else {
const totalDocument = await Urls.countDocuments({});
const newUrl = await Urls.create({
original_url: urlString,
short_url: totalDocument + 1,
});
return res.json(newUrl);
}
});
} catch (e) {
return res.json({
error: "Invalid URL",
});
}
});
mongoose
.connect(
"mongodb+srv://abbarzukhrofy:mamasasa@zukhrofy.xvvdwqj.mongodb.net/URL-Shortener-Microservice",
)
.then(() => {
// listen for requests
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
});