-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (61 loc) · 1.82 KB
/
server.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
// import shits
import express from "express";
// import SHORTURL_MODEL from "./models/shortUrl.js";
// import USER_MODEL from "./models/user.js";
import mongoose from "mongoose";
// import bcrypt from "bcrypt";
// import jwt from "jsonwebtoken";
import cookieParser from "cookie-parser";
//import controllers
import { loginGet, loginPost } from "./controllers/login.controller.js";
import {
createShortUrl,
deleteUrl,
redirectShortUrl,
} from "./controllers/url.controller.js";
import { getHome } from "./controllers/home.controller.js";
// import middleware
import verifyToken from "./middleware/authorize.js";
// import path from 'path'
// import fs from 'fs'
// import { fileURLToPath } from 'url'
// get env
const app = express();
const PORT = process.env.PORT;
const mongoURI = process.env.MONGODB_URI;
//connect ot mongodb
const connectMongoDB = async (mongoURI) => {
try {
await mongoose.connect(mongoURI);
console.log("Successfully Connected to MongoDB !!");
} catch (error) {
console.log(error);
process.exit(1);
}
};
// const __dirname = import.meta.dirname;
// const __filename = import.meta.filename;
//add ejs view engine
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
// to use css in ejs put this and add output.css
app.use(express.static("src"));
// allow use of cookies
app.use(cookieParser());
// login hahahahah
// This will create a jwt and save to cookie if entered correct pass and username
app.get("/login", loginGet);
// post username and pass
app.post("/login", loginPost);
// get Home
app.get("/", verifyToken, getHome);
// create
app.post("/shortUrls", createShortUrl);
// redirect
app.get("/:shortUrl", redirectShortUrl);
// delete
app.post("/:id", deleteUrl);
app.listen(PORT, () => {
connectMongoDB(mongoURI);
console.log(`Server running in PORT ${PORT}`);
});