-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (52 loc) · 1.74 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
57
58
59
60
61
62
63
64
65
66
67
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const chalk = require('chalk');
const connected = chalk.bold.cyan;
const error = chalk.bold.yellow;
const disconnected = chalk.bold.red;
const termination = chalk.bold.magenta;
const db = require('./config/db'); //getting db info
mongoose.connect(db.dbURL,{ useNewUrlParser: true, useUnifiedTopology: true });
const connection = mongoose.connection;
connection.on('connected', function(){
console.log(connected("Mongoose default connection is open to ", db.dbURL));
});
connection.on('error', function(err){
console.log(error("Mongoose default connection has occured "+err+" error"));
});
connection.on('disconnected', function(){
console.log(disconnected("Mongoose default connection is disconnected"));
});
process.on('SIGINT', function(){
mongoose.connection.close(function(){
console.log(termination("Mongoose default connection is disconnected due to application termination"));
process.exit(0);
});
});
app.use(express.urlencoded({ extended: true }));
var UserSchema = new mongoose.Schema({
firstname: String,
lastname: String
});
var Customer = mongoose.model('Customer',UserSchema, 'data' );
app.get('/api/customers', (req, res) => {
// const customers = [
// {id: 1, firstName: 'John', lastName: 'Doe'},
// {id: 2, firstName: 'Jane', lastName: 'Smith'},
// {id: 3, firstName: 'Sam', lastName: 'Swanson'},
// ];
Customer.find({}, function(err, data){
if(err) {
res.json(err);
}
else {
res.json(data);
}
});
// res.json(customers);
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(connected(`Server is running on PORT ${PORT}`));
});