-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (50 loc) · 1.84 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
import express from 'express';
import { loginRestrict, userAccountRestrict } from './middleware/auth.js';
import { router } from './routes/url.js';
import userRouter from './routes/user.js';
import mongoDBConnection from './connection.js';
import { urls } from './models/url.js';
import cookieParser from 'cookie-parser';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
dotenv.config().parsed;
const app = express();
const port = process.env.PORT || 4000;
// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
mongoDBConnection(process.env.MONGO_URL)
.then(() => { console.log('Connected to MongoDB'); })
.catch((err) => { console.log('Error: ', err); });
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../public')));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.get('/', userAccountRestrict, async (req, res) => {
if (!req.user) return res.redirect('/login');
const showUrl = await urls.find({ createdBy: req.user._id });
return res.render('index', { url: showUrl, id: req.user.name })
});
app.get('/login', (req, res) => {
res.render('login');
});
app.get('/register', (req, res) => {
res.render('register');
});
app.get('/:shortUrl', async (req, res) => {
const shortUrl = req.params.shortUrl;
const entry = await urls.findOneAndUpdate(
{ shortUrl },
{ $push: { clicks: { timestamp: Date.now() } } }
);
if (!entry) return res.render('error');
res.redirect(entry.url);
})
app.use('/url', loginRestrict, router);
app.use('/user', userRouter);
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});