-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (59 loc) · 1.98 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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cors = require('cors');
const dns = require('dns');
const urlParser = require('url');
const { url } = require('inspector');
const app = express();
// Basic Configuration
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
mongoose.connect(process.env.cluster,{ useNewUrlParser: true, useUnifiedTopology: true }).then( () => console.log("Connection Successfull\n","Connection Status: ",
mongoose.connection.readyState)).catch(console.log("Database Error\n","Connection Status: ",mongoose.connection.readyState));
app.use(cors());
app.use('/public', express.static(`${process.cwd()}/public`));
app.get('/', function(req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});
// Your first API endpoint
app.get('/api/hello', function(req, res) {
res.json({ greeting: 'hello API' });
});
// making urlSchema in mongoose.
const urlSchema = new mongoose.Schema({url : String});
// making urlSchema model.
const URL = new mongoose.model('URL', urlSchema);
app.post('/api/shorturl', (req, res) => {
let body = req.body.url;
dns.lookup(urlParser.parse(body).hostname, (err, address) => {
if (!address) {
res.json({error: 'invalid url' });
}
else {
URL.findOne({url: body}, (err, data) => {
if(data) {
res.json({original_url: data.url, short_url: data._id });
}
else {
URL.create({url : body}, (err, data) => {
if (err) return console.error(err);
res.json({original_url: data.url, short_url: data._id }); });
}
});
}
});
});
app.get('/api/shorturl/:id', (req, res) => {
let num = req.params.id;
URL.findById(num, (err, data) => {
if(!err) {
res.redirect(data.url);
}
});
});
app.listen(port, function() {
console.log(`Listening on port ${port}`);
});