-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (92 loc) · 2.91 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint no-console: 0 */
'use strict';
/////
require('dotenv').config();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const configObj = require('./config/config');
const logger = require('./helpers/logger');
const cors = require('cors');
const express = require('express');
const path = require('path');
const http = require('http');
const requestify = require('requestify');
const async = require('async');
const utils = require('./helpers/utils');
const serviceHelper = require('./helpers/serviceHelper');
const Person = require('./models/person').Person;
const app = express();
app.use(bodyParser.json());
app.use(cors());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'bower_components')));
app.use(express.static(path.join(__dirname, 'public')));
app.set('port', configObj.PORT || 3000);
const baseUrl = 'http://localhost:' + app.get('port');
const dummyObj = {
prop1: 'this is prop1',
prop2: 42,
};
mongoose.connect(configObj.DATABASE_CONN_DETAILS);
// mongoose.set('debug', true);
http.createServer(app).listen(app.get('port'), () => {
logger.info('express server started on port : %d', app.get('port'));
async.waterfall([
(callback) => {
logger.debug('calling serviceHelper.deleteAllPeople');
serviceHelper.deleteAllPeople(callback);
},
(callback) => {
logger.debug('calling requestify.post');
requestify.post(baseUrl + '/savePerson', {
firstName: 'jimmy',
lastName: 'smith',
address: '123 Any Road, Omaha NE 12345',
age: 34,
}).then((res) => {
console.log('inside the "then" handler');
console.log('res.getBody() = ' + JSON.stringify(res.getBody()));
callback(null);
}, (err) => {
console.log('An error occurred, ' + err.body);
callback(err);
});
},
], (err, result) => {
if (err) {
console.log('error encountered: ' + JSON.stringify(err));
} else {
console.log('result = ' + result);
}
});
});
app.use((req, res, next) => {
req.dummyObj = dummyObj; // add dummyObj to request
next();
});
app.get('/', (req, res) => {
console.log('req.dummyObj = ' + JSON.stringify(req.dummyObj));
res.contentType('text/html');
res.sendFile(path.join(__dirname, '/html', 'main.html'));
});
app.get('/getAllPeople', (req, res) => {
serviceHelper.getAllPeople((people) => {
res.send(people);
});
});
app.get('/addPerson', (req, res) => {
res.contentType('text/html');
res.sendFile(path.join(__dirname, '/html', 'addPerson.html'));
});
app.post('/savePerson', (req, res) => {
console.log('req.body = %s\n', JSON.stringify(req.body));
async.waterfall([
(callback) => {
serviceHelper.createModel(Person, 'Person', req.body, callback);
},
], (err, result) => {
utils.processResponse(err, result, res);
});
});
module.exports = app;