-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (41 loc) · 1.25 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
import express from 'express';
import path from 'path';
import connectMongoDb from './connectMongoDB.js';
import cookieParser from 'cookie-parser';
import { restrictToLoggedInUserOnly, checkAuth } from './middleware/auth.js';
import urlRoute from './routes/urlRoute.js';
import URL from './model/urlModel.js';
import staticRouter from './routes/staticRouter.js';
import userRouter from './routes/user.js';
const app = express();
const PORT = 8000;
connectMongoDb(
'mongodb+srv://sutharjay3635:[email protected]/'
).then(() => {
console.log('MongoDB connected');
});
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.set('view engine', 'ejs');
app.set('views', path.resolve('./views'));
app.get('/url/:shortID', async (req, res) => {
const shortID = req.params.shortID;
const entry = await URL.findOneAndUpdate(
{ shortID },
{
$push: {
visitHistory: {
timestamp: Date.now(),
},
},
}
);
res.redirect(entry.redirectUrl);
});
app.use('/url', restrictToLoggedInUserOnly, urlRoute);
app.use('/user', userRouter);
app.use('/', checkAuth, staticRouter);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});