-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (54 loc) · 1.63 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
56
57
58
59
60
61
62
63
64
65
66
67
import express, { Express, Request, Response } from "express";
import dotenv from "dotenv";
import mongoose from "mongoose";
import cors, { CorsOptions } from "cors";
import morgan from "morgan";
import helmet from "helmet";
import bodyParser from "body-parser";
import userRouter from "./routes/user";
import authRouter from './routes/auth';
import storeRouter from './routes/store';
import purchaseRouter from './routes/purchase';
dotenv.config();
const app: Express = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json())
// const allowedOrigins: string[] = ['https://www.kmlapay.com', 'http://localhost:3000'];
/*
const corsOptions: CorsOptions = {
origin: (origin, callback) => {
if (allowedOrigins.includes(origin || '')) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
};
*/
// app.use(cors(corsOptions));
app.use(cors());
app.use(morgan("dev"));
app.use(helmet());
app.use(bodyParser.json());
app.get("/", (req: Request, res: Response) => {
res.send("Express + TypeScript Server");
});
const connect = async () => {
try {
await mongoose.connect(process.env.MONGO || "");
console.log("Connected To MongoDB");
} catch (error) {
throw error;
}
};
mongoose.connection.on("disconnected", () => {
console.log("MongoDB Disconnected");
});
app.use("/api/user", userRouter);
app.use("/api/auth", authRouter);
app.use("/api/store", storeRouter);
app.use("/api/purchase", purchaseRouter);
app.listen(process.env.PORT || 8800, () => {
connect();
console.log(`[server]: Server is running at http://localhost:${process.env.PORT || 8800}`);
});