-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·214 lines (131 loc) · 3.21 KB
/
index.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var express = require('express')
var path = require('path')
var giphy = require('giphy-api')()
var bodyParser = require('body-parser')
var app = express()
var http = require('http')
http = http.Server(app)
var io = require('socket.io')(http)
var mongoose = require('mongoose')
// connect to the db
mongoose.connect("mongodb://hello:hello@ds037234.mlab.com:37234/workshop");
var db = mongoose.connection;
db.on('open', function(){
console.log("I have connected to the database")
})
var Schema = mongoose.Schema;
var userSchema = new Schema({
name: {
type: String,
trim: true
}
})
var User = mongoose.model('User', userSchema)
// MIDDLEWARE
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine','pug')
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', function(req, res){
var name = "Abhishek"
// get name from database
// add first name and last name
// set it to a variable
// pass that vairable to the template
res.render("index.pug", {myname:name})
})
app.get('/gif', function(req, res){
var searchQuery = req.query.search
var myduration = req.query.duration
giphy.search(searchQuery, function(err, result){
res.send(result.data[0].url)
})
})
app.get('/search/:thing', function(req, res){
var searchQuery = req.params.thing
giphy.search(searchQuery, function(req, result){
res.send(result.data[0].images.fixed_height.url)
})
})
app.get('/give-me-a-gif', function(req, res){
giphy.search('superman', function(err, result){
res.send(result.data[0].url)
})
})
app.get('/user', function(req, res){
// save a user to the database
var newUser = new User({
name: "Hayley"
})
newUser.save(function(err,result){
if(err){
console.log(err)
res.send("Error saving your name")
} else {
res.send("Saved to database!")
}
})
})
app.get('/filter', function(req, res){
User.find({name:"paula"}, function(err, result){
return res.json(
{
data: result
}
)
})
})
app.post('/newuser', function(req, res, next){
var newname = req.body.name
var newuser = new User({
name: newname
})
newuser.save(function(err, result){
if(err){
res.send("could not save")
} else {
res.send("saved to database! - ajax")
}
})
})
app.get('/all', function(req, res){
// show me all the currently saved users
User.find({}, function(err, results){
if(err){
res.send("caanot find anyone")
}
return res.json(
{
data: results
}
)
})
})
app.get('/about', function(req, res){
var list = ["chris","leslie","paula","jenny","ondina","patrick"]
var meeting = false;
res.render("about.pug", {namelist: list, is_meeting_on: meeting})
})
app.get('/contact', function(req, res){
res.send("contact page")
})
io.on('connection', function(socket){
console.log("new client connected")
socket.on("chat",function(data){
io.emit("outgoing-message", data) // -> every
// socket.emit() -> only
})
socket.on("click",function(data){
console.log(data)
})
socket.on("message",function(data){
console.log(data)
socket.emit("resend",data)
})
})
var server = http.listen(3000, function(){
console.log("I am listening on port 3000")
})