-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·49 lines (41 loc) · 1.24 KB
/
app.js
File metadata and controls
executable file
·49 lines (41 loc) · 1.24 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
// Load express module
const express = require('express');
// Initialize app
const app = express();
// Initialize port
const port = process.env.PORT || 5000;
// Initialize paths
const path = require('path');
// Initialize public directory
app.use(express.static(path.join(__dirname, 'public')));
// Mongoose connection
const mongoose = require('mongoose');
mongoose.connect('mongodb://salitha:salitha94@ds111622.mlab.com:11622/node_api');
const db = mongoose.connection;
// Check for DB connection
db.once('open', function(){
console.log("Connected to MongoDB successfully!");
});
db.on('error', function(){
console.log(err);
});
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Initialize CORS middleware
const cors = require('cors');
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
})
// export Routes
const users = require('./routes/router');
app.use('/api', users);
// Route for home
app.get('/', function(req, res){
res.send('Hello from Server');
})
app.listen(port, function(){
console.log('server started...');
})