-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
65 lines (50 loc) · 1.59 KB
/
server.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
import express, { Request, Response } from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import bodyParser from 'body-parser';
import winston from "winston";
import userRoutes from './server/user/routes';
import vehicleRoutes from './server/vehicle/routes';
const mongoose = require('mongoose');
dotenv.config();
const app = express();
const port = process.env.PORT || 3030;
app.use(cors());
app.use(bodyParser.json({ limit: "1mb" }));
app.use(bodyParser.urlencoded({ extended: true }));
export const logger = winston.createLogger({
// Log only if level is less than (meaning more severe) or equal to this
level: "info",
// Use timestamp and printf to create a standard log format
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(
(info: any) => `${info.timestamp} ${info.level}: ${info.message}`
)
),
transports: [
new winston.transports.Console()
]
});
// MongoDB connection URL
const mongoURL = process.env.DB_URL;
// Connect to MongoDB
mongoose.connect(mongoURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
logger.log("info", "Connected to MongoDB");
})
.catch((error: Error) => {
logger.log("error", `Error connecting to MongoDB: ${error}`);
});
app.get("/", (req: Request, res: Response) => {
res.status(200).send("Welcome to LIS API!");
})
app.use('/user', userRoutes);
app.use('/vehicles', vehicleRoutes);
// Start the server
app.listen(port, () => {
logger.log("info", `Server is running on port ${port}!`);
});