-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiApp.js
35 lines (26 loc) · 994 Bytes
/
apiApp.js
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
require('dotenv').config()
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const movieRoutes = require('./routes/movieRoutes');
const app = express();
const port = process.env.PORT || 3000;
// Replace username, password and database in the .env file by your
// own login credentials and database to connect to your MongoDB database
// Otherwise, you can also replace it by the url to an other database of your own choosing
mongoose.connect(`mongodb+srv://${process.env.USER}:${process.env.PASSWORD}@${process.env.DATABASE}.mongodb.net/?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.use(express.json());
app.use(cors());
app.use('/movies', movieRoutes);
try {app.listen(port, () => {
console.log(`API LAUNCH: \x1b[42mSUCCESS\x1b[0m`);
console.log(`Server is running on port ${port}`);
});
}
catch (error) {
console.log(`API LAUNCH: \x1b[41mFAIL\x1b[0m`);
console.log(error);
}