-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
55 lines (42 loc) · 1.43 KB
/
index.ts
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
import "dotenv/config";
import express, { Express, Request, Response, NextFunction } from 'express';
import mongoose from 'mongoose';
import jwt from 'jsonwebtoken';
import users from "./routes/api/users";
import payments from "./routes/api/payments";
// Database Configuration
mongoose.connect(`${process.env.MONGODB_URI}`);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "Error connecting to database: "));
db.once("open", () => {
console.log("Mongo DB connected successfully.");
});
const HTTP_PORT = process.env.PORT || 8080;
const app: Express = express();
/*
middlewares
*/
function authenticate(req: Request, res: Response, next: NextFunction) {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (token === null || token === undefined) return res.sendStatus(401);
jwt.verify(`${token}`, `${process.env.ACCESS_TOKEN_SECRET}`, (err, user) => {
if(err) return res.sendStatus(403)
// @ts-ignore
req.user = user;
next();
})
}
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public/assets'));
/*
route endpoints
*/
app.use("/api/users", authenticate, users);
app.use('/api/payment', payments);
/* server listening*/
const onHttpStart = () => {
console.log(`The web server has started at http://localhost:${HTTP_PORT}`);
console.log("Press CTRL+C to stop the server.");
};
app.listen(HTTP_PORT, onHttpStart);