-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (102 loc) · 3.54 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Module dependencies.
*/
// var express = require('express');
var mongoose = require('mongoose');
var request = require('request');
var feathers = require('feathers');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var selfService = require('./modules/Self-Service/lib');
// Create Self-Service object
var s = new selfService;
// Connect to Mongo
var connection = mongoose.connect('mongodb://localhost/test3');
/*var db = mongoose.connection();
db.on('open', function () {
// now we can start talking
});*/
// Server
var app = feathers();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(feathers.favicon());
app.use(feathers.logger('dev'));
app.use(feathers.json());
app.use(feathers.urlencoded());
app.use(feathers.methodOverride());
app.use(app.router);
app.use('/bower_components', feathers.static(__dirname + '/bower_components'));
app.use('/node_modules', feathers.static(__dirname + '/node_modules'));
app.use(feathers.static(path.join(__dirname, 'public')));
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', "*");
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
// development only
if ('development' == app.get('env')) {
app.use(feathers.errorHandler());
}
//------------------------------ Mongoose Models ---------------------------//
app.get('/', routes.index);
app.get('/users', user.list);
app.post('/signup', function(req, res) {
var username = req.body.id;
var password = req.body.pass;
s.login({'username': username, 'password': password }, function(error, response, localService) {
if (error != null){}
else{
localService.detailSchedule({ /*'startDate': new Date()*/ }, function(error, response, courses) {
console.log("Completed!", courses);
var options = {
header: {"content-type":"application/json"},
method: 'POST',
uri: 'http://localhost:3000/api/users',
json: {"A": username, "courses": courses}
};
request(options, function(error, response, body){
if (!error && response.statusCode == 200) {
console.log(body);
}
});
});
//res.send(courses)
//res.redirect('/welcome.html');
}
});
var query = "?A=" + username;
res.redirect("welcome.html" + query);
/* options = {
method: 'GET',
url: 'http://localhost:3000/welcome.html',
qs: {"A": username}
};
console.log("A="+username);
res.send(request(options, function(error,response, body){
console.log("A="+username);
//res.send(body);
}) ); */
});
var UsersService = require('./db.js')(connection);
app.configure(feathers.socketio())
.use(feathers.static(__dirname))
.use('/api/users', new UsersService())
.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
/*http.createServer(app).listen(app.get('port'), function(){
console.log('feathers server listening on port ' + app.get('port'));
});*/