-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (44 loc) · 1.38 KB
/
server.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const cors = require('cors');
const expressEjsLayouts = require('express-ejs-layouts');
// connecting dotenv for my project
require('dotenv').config();
// giving port
const port = process.env.port || 8000;
// my express app
const app = express();
// using cors for accessing cross origin browsers datas
app.use(cors());
// setup url encoder
app.use(express.urlencoded());
// setup layouts for express_EJS
app.use('/', expressEjsLayouts);
// accessing front-end datas and files
app.use(express.static('assets'));
// setup my view engine for front-end
app.set('view engine', 'ejs');
app.set('views', './views');
// accessing routes in app
app.use('/', require('./routers'));
// method for firing server
module.exports.startServer = async () => {
try {
// DataBase connection with mongoDB
await mongoose.connect(process.env.mongoDbURL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log("Connected with :: MongoDB")
// firing server
app.listen(port, (err) =>{
if(err) {
throw new Error(err);
}
console.log(`${process.env.environment} server starts at port ${port}`);
})
} catch (error) {
console.log('error', error);
}
}