-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
48 lines (39 loc) · 1.32 KB
/
app.ts
File metadata and controls
48 lines (39 loc) · 1.32 KB
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
import express from 'express';
import cors from 'cors';
import ordersRoutes from './api/routes/orders';
import productRoutes from './api/routes/products';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import mongoose from 'mongoose';
import userRoutes from './api/routes/user'
import Response from './api/response';
const mongodbURL: string = 'mongodb+srv://' + process.env.MONGO_ID + ':' + process.env.MONGO_PASSWORD + '@testcluster-trljw.mongodb.net/test?retryWrites=true'
const app = express();
//MongoDB Setup with Mongoose
mongoose.connect(mongodbURL, { useNewUrlParser: true });
//Body Parser Middlewares
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
//Cross Origin Middleware
app.use(cors());
//Static Files
app.use('/uploads', express.static('uploads'))
//Logging Middleware
app.use(morgan('dev'))
//Router Middlewares
app.use('/users', userRoutes)
app.use('/orders', ordersRoutes);
app.use('/products', productRoutes);
//Error Middlewares
app.use((req, res, next) => {
let error: any = new Error('Page Not Found');
error.status = 404;
next(error)
});
app.use((error: any, req: any, res: any, next: any) => {
var response = new Response();
res.status(error.status || 500);
response.error.push(error.message);
res.json(response)
});
export default app;