-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
51 lines (43 loc) · 1.19 KB
/
app.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
import express from "express";
const app = express();
import cors from "cors";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import { mongoURI, mongoOptions } from "./config";
// variables
const PORT = 4000;
// middleware
app.use(cors());
app.use(express.json());
app.use(express.static("./public"));
async function connectDatabase() {
try {
await mongoose.connect(mongoURI, mongoOptions);
console.log("connected to mongo database...");
} catch (error) {
if (error instanceof Error) {
console.log(`connection to database error: ${error.message}`);
}
process.exit();
}
}
async function connect() {
mongoose
.connect(mongoURI, mongoOptions)
.then((res) => console.log("connected to mongoDB"))
.catch((err) => {
console.log(err);
});
}
connect();
//routes
app.use("/api/users", require("./routes/users.ts"));
app.use("/api/auth", require("./routes/auth.ts"));
app.use("/api/account", require("./routes/account.ts"));
app.use("/api/photos", require("./routes/photos.ts"));
app.get("/", (req, res) => {
res.send("routes: users, auth, account");
});
app.listen(PORT, () => {
console.log(`server is listening on port ${PORT}...`);
});