-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (42 loc) · 1.28 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
const express = require('express');
require('dotenv').config();
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const todoRoutes = express.Router();
const PORT = process.env.PORT || 4000;
let Cred = require('./cred.model');
app.use(cors());
app.use(bodyParser.json());
mongoose.connect(process.env.DATABASE, ) //{useUnifiedTopology: true, useNewUrlParser: true}
const connection = mongoose.connection;
connection.once('open', function() {
console.log("MongoDB DataBase Connection Established Successfully")
})
todoRoutes.route('/').get(function(req, res){
Cred.find()
.then(result => {
console.log(result);
res.json(result);
})
.catch(error => {
// Handle error
console.log(error);
});
});
todoRoutes.route('/add').post(function(req, res){
let cred = new Cred(req.body);
console.log(JSON.stringify(cred))
cred.save()
.then(cred => {
res.status(200).json({'cred' : 'cred added successfully'});
})
.catch(err => {
res.status(400).send('adding new cred Failed');
});
});
app.use('/instagram', todoRoutes);
app.listen(PORT, function () {
console.log("Server is running on Port: " + PORT);
});