-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (62 loc) · 1.97 KB
/
server.js
File metadata and controls
73 lines (62 loc) · 1.97 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { errorHandler, urlNotFound } from './middleware/error.middleware.js';
import UserRoutes from './routes/user.routes.js';
import authRoutes from './routes/auth.routes.js';
// this is required, even though not used
import colors from 'colors';
import { connectDB } from './config/db.js';
import cors from 'cors';
import dotenv from 'dotenv';
import express from 'express';
import morgan from 'morgan';
import orderRoutes from './routes/order.routes.js';
import path from 'path';
import productRoutes from './routes/product.routes.js';
import reviewRoutes from './routes/review.routes.js';
import uploadRoutes from './routes/upload.routes.js';
dotenv.config();
connectDB();
const app = express();
const corOptions = {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
};
// configure third party middleware
app.use(cors(corOptions));
// Log HTTP methods to console
app.use(morgan('dev'));
app.use(express.urlencoded());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use('/api/products', productRoutes);
app.use('/api/auth', authRoutes);
app.use('/api/users', UserRoutes);
app.use('/api/orders', orderRoutes);
app.use('/api/upload', uploadRoutes);
app.use('/api/reviews', reviewRoutes);
app.use('/api/config/paypal', (req, res) =>
res.send(process.env.PAYPAL_CLIENT_ID),
);
// mimic __dirname from common js which
// insint available in the type: module synthax
const __dirname = path.resolve();
// this is how we can make a folder static
app.use('/uploads', express.static(path.join(__dirname, '/uploads')));
app.use('/', (req, res) => res.json({ message: 'Welcome to Avio api' }));
app.use(urlNotFound);
app.use(errorHandler);
let port;
if (process.env.NODE_ENV === 'test') {
port = 7000;
} else {
port = process.env.PORT || 6000;
}
app.listen(
port,
console.log(
`The Server is running in ${process.env.NODE_ENV} mode on port ${port}`.cyan
.underline,
),
);
export default app;