forked from freeCodeCamp/boilerplate-project-urlshortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
104 lines (84 loc) · 2.88 KB
/
server.mjs
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
94
95
96
97
98
99
100
101
102
103
104
// server.js
// where your node app starts
// init project
import dns from 'dns';
import express from 'express';
import cors from 'cors';
import mongoose from 'mongoose';
import mongooseSequence from 'mongoose-sequence';
import bodyParser from 'body-parser';
import dotenv from 'dotenv';
import { gDirname } from './utils/utilityFunctions.mjs';
dotenv.config();
const app = express();
/** this project needs a db !! * */
mongoose.connect(process.env.MONGOLAB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connection.on('error', err => {
console.log(err);
});
const AutoIncrement = mongooseSequence(mongoose);
const urlSchema = new mongoose.Schema({
url: { type: String, require: true },
});
urlSchema.plugin(AutoIncrement, { inc_field: 'id' });
const Url = mongoose.model('Url', urlSchema);
// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC
app.use(cors({ optionSuccessStatus: 200 })); // some legacy browsers choke on 204
/** this project needs to parse POST bodies * */
app.use(bodyParser.urlencoded({ extended: false }));
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', (req, res) => {
res.sendFile(`${gDirname(import.meta.url)}/views/index.html`);
});
// your first API endpoint...
app.get('/api/hello', (req, res) => {
res.json({ greeting: 'hello API' });
});
app.get('/api/shorturl/:urlNumber', (req, res) => {
if (Number(req.params.urlNumber)) {
Url.findOne({ id: parseInt(req.params.urlNumber, 10) }, (err, data) => {
if (err || !data) {
res.json({ error: 'invalid URL' });
} else {
res.redirect(data.url);
}
});
}
});
app.post('/api/shorturl/new', (req, res) => {
if (req.body.url.match(/^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/)) {
const startUrl = req.body.url.indexOf('://') + 3;
const endUrl = req.body.url.indexOf('/', startUrl);
const shortUrl = req.body.url.substring(startUrl, endUrl > startUrl ? endUrl : undefined);
dns.lookup(shortUrl, err => {
if (err) {
res.json({ error: 'invalid URL' });
} else {
Url.findOne({ url: req.body.url }, (error, data) => {
if (!data) {
const newUrl = new Url({ url: req.body.url });
newUrl.save((e, d) => {
if (e) {
res.json({ error: e });
} else {
res.json({ url: d.url, shortUrl: d.id });
}
});
} else {
res.json({ url: data.url, shortUrl: data.id });
}
});
}
});
} else {
res.json({ error: 'invalid URL' });
}
});
// listen for requests :)
const port = process.env.PORT || 3000;
const listener = app.listen(port, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});