-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (26 loc) · 1.01 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
const express = require('express');
const path = require('path')
const cookieParser = require('cookie-parser')
const {connectMongo} = require('./connection.js');
const staticRouter = require('./routes/static')
const urlRouter = require('./routes/url');
const userRouter = require('./routes/user');
const { restrictToLoginUserOnly, checkAuth } = require('./middlewares/auth.js');
const app = express();
const PORT = 5001;
//connect MongoDB
connectMongo("mongodb://127.0.0.1:27017/urlShortner1").then(() => console.log("MongoDb connected!"))
app.set("view engine", "ejs")
app.set("views", path.resolve('./views'))
// Middleware
app.use(express.json());
app.use(express.urlencoded({extended: false}))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
//Routes
// app.use('/', staticRouter);
app.use('/', checkAuth, staticRouter)
app.use('/user', userRouter)
app.use('/api/url', restrictToLoginUserOnly, urlRouter);
//Server started
app.listen(PORT, () => console.log(`Server started at: ${PORT}`))