-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
44 lines (34 loc) · 1.02 KB
/
app.js
File metadata and controls
44 lines (34 loc) · 1.02 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
const cors = require('cors');
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config();
const authRoutes = require('./routes/auth');
const productRoutes = require('./routes/product');
const pageNotFound = require('./middlewares/404');
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get('/', (req, res) => {
res.send('App is running!');
});
// auth routes
app.use('/api/user', authRoutes);
// products routes
app.use('/api/products', productRoutes);
// page not found or 404 middleware
app.use(pageNotFound);
async function createDBConnection() {
try {
if (process.env.NODE_ENV !== 'TEST') {
await mongoose.connect(process.env.MONGODB_URL);
app.listen(process.env.PORT || 5000, () =>
console.log(`app running on port ${process.env.PORT}!`)
);
}
} catch (error) {
console.log(error, 'Database connection failed.');
}
}
createDBConnection();
module.exports = app; // for testing