-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
181 lines (172 loc) · 5.03 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var express = require("express");
var app = express();
var dbConfig = require('./config.js')();
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var Sequelize = require("sequelize");
//var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '/public')));
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();
});
//logger for express
app.use(logger('combined'));
var sequelize = new Sequelize(dbConfig.dbName, dbConfig.dbUserName, dbConfig.dbPassword, {
host: dbConfig.host,
//port : '3306',
dialect: 'mysql',
pool: {
max: 100,
min: 0,
idle: 1000
},
});
sequelize.authenticate().then(function (err) {
console.log("connection established");
}).catch(function (err) {
console.log("Unable to connect to database" + err);
});
sequelize.sync();
var Student = sequelize.import('./models/student.model.js');
var Event = sequelize.import('./models/event.model.js');
Student.belongsToMany(Event, {
through: 'registerations'
});
Event.belongsToMany(Student, {
through: 'registerations'
});
//Routes for rendering files.
app.get('/contact', function (req, res) {
res.sendFile('/contact.html', {
root: './public'
});
});
app.get('/clubs', function (req, res) {
res.sendFile('/clubs.html', {
root: './public'
});
});
app.get('/contri', function (req, res) {
res.redirect("/contributions");
});
app.get('/contributions', function (req, res) {
res.sendFile('/contri.html', {
root: './public'
});
});
app.get('/events', function (req, res) {
res.sendFile('/events.html', {
root: './public'
});
});
app.get('/register', function (req, res) {
res.sendFile('/register.html', {
root: './public'
});
});
app.get('/team', function (req, res) {
res.sendFile('/team.html', {
root: './public'
});
})
//Functional Routes.
// accepting an object {email : **,number: ** , year : ** , name: ** ,admissionNumber : **};
app.post('/api/studentRegister', function (req, res) {
//console.log("req.body.email is " +req.body.email);
var email = req.body.email;
var contactNo = req.body.number;
var year = req.body.year;
var name = req.body.name;
var admissionNumber = req.body.admissionNumber;
// var event_id = req.body.event_id;
var event_id;
Event.findOne({
order: 'start_time DESC'
}).then(function (result) {
event_id = result.id;
Student.findOrCreate({
where: {
'email': email
},
defaults: {
'contact_no': contactNo,
'year': year,
'name': name,
'admission_no': admissionNumber
}
}).spread(function (student, created) {
//console.log(student);
student.addEvent(event_id);
console.log("added an entry in registrations");
console.log(student.get({
plain: true
}))
console.log("Created At student Table " + created);
})
res.send("studentCreaded is ");
});
//console.log(req.body);
});
//Used to get all the routes.
app.get('/api/getevents', function (req, res) {
Event.all({
order: 'start_time DESC'
}).then(events => res.status(200).send(events)).catch(error => {
res.status(400).send(error);
console.log(error)
});
});
// app.post("/event", function(req, res) {
// var eventName = req.body.eventName;
// var displayStartTime = req.body.displayStartTime;
// var displayEndTime = req.body.displayEndTime;
// Event.findOrCreate({
// where: {
// 'eventName': eventName
// },
// defaults: {
// 'displayEndTime': displayEndTime,
// 'displayStartTime': displayStartTime
// }
// }).spread(function(events, created) {
// //console.log(student);
// console.log(events.get({
// plain: true
// }))
// res.send("created!");
// })
// })
// to get the events whose poster are to be live @current time.
app.get('/api/upcomingEvent', function (req, res) {
Event.findOne({
order: 'start_time DESC'
}).then(function (result) {
console.log(result);
res.send(result);
});
});
app.get('/api/events/:id', function (req, res) {
Event.findOne({
id: req.params.id
}).then(function (result) {
res.send(result);
});
});
app.get('/', function (req, res) {
console.log(__dirname + '/public');
res.sendFile('/index.html', {
root: __dirname + '\\public'
});
});
app.listen(3000, function () {
console.log("we are listening at port 3000\n type http://localhost:3000/ in chrome");
});