-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
204 lines (167 loc) · 5.57 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require('dotenv').config({ path: '.env' });
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const Pusher = require('pusher');
var request = require('request');
const app = express();
const server1 = require("http").Server(app);
const io = require("socket.io")(server1);
io.on("connection", socket => {
const { id } = socket.client;
console.log(`User Connected: ${id}`);
socket.on("chat message", ({ nickname, msg }) => {
io.emit("chat message", { nickname, msg });
});
});
const pusher = new Pusher({
appId: process.env.PUSHER_APP_ID,
key: process.env.PUSHER_APP_KEY,
secret: process.env.PUSHER_APP_SECRET,
cluster: process.env.PUSHER_APP_CLUSTER,
useTLS: true,
});
// Exprees will serve up production assets
// Express serve up index.html file if it doesn't recognize route
const path = require('path');
app.use(express.static(path.join(__dirname, 'client/build')));
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/update-editor', (req, res) => {
pusher.trigger(req.body.room, 'text-update', {
...req.body,
});
res.status(200).send('OK');
});
app.post('/pusher/auth', function(req, res) { // authenticate user's who's trying to connect
var socketId = req.body.socket_id;
var channel = req.body.channel_name;
var auth = pusher.authenticate(socketId, channel);
res.send(auth);
});
// Post request to compile the code
app.post('/editor', (req, res) => {
console.log(req.body);
// hackerearth api secret key
var CLIENT_SECRET = "926a0a861df9fc10c5cd44d16d3b12cf1a0aef2c";
// data to be sent to the hackerearth api
var requ ={
url: "https://api.hackerearth.com/v3/code/run/",
method: 'POST',
form: {
'client_secret': CLIENT_SECRET,
'async': 0,
'source': req.body.source,
'lang': req.body.lang,
'time_limit': 5,
'memory_limit': 262144,
}
};
// sending request to the hackerearth api
request(requ, async (err, resp, body) => {
// Convert data to json
body = JSON.parse(body);
res.send(body);
});
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
app.set('port', process.env.PORT || 5000);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});
/*
require('dotenv').config({ path: '.env' });
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const Pusher = require('pusher');
var request = require('request');
const app = express();
const http = require('http');
const socketio = require('socket.io');
const { addUser, removeUser, getUser, getUsersInRoom } = require('./users');
const router = require('./router');
const server = http.createServer(app);
const io = socketio(server);
app.use(cors());
app.use(router);
const pusher = new Pusher({
appId: process.env.PUSHER_APP_ID,
key: process.env.PUSHER_APP_KEY,
secret: process.env.PUSHER_APP_SECRET,
cluster: process.env.PUSHER_APP_CLUSTER,
useTLS: true,
});
// Exprees will serve up production assets
// Express serve up index.html file if it doesn't recognize route
const path = require('path');
app.use(express.static(path.join(__dirname, 'client/build')));
io.on('connect', (socket) => {
socket.on('join', ({ name, room }, callback) => {
const { error, user } = addUser({ id: socket.id, name, room });
if(error) return callback(error);
socket.join(user.room);
console.log(user.name);
socket.emit('message', { user: 'admin', text: `${user.name}, welcome to room ${user.room}.`});
socket.broadcast.to(user.room).emit('message', { user: 'admin', text: `${user.name} has joined!` });
io.to(user.room).emit('roomData', { room: user.room, users: getUsersInRoom(user.room) });
callback();
});
socket.on('sendMessage', (message, callback) => {
const user = getUser(socket.id);
io.to(user.room).emit('message', { user: user.name, text: message });
callback();
});
socket.on('disconnect', () => {
const user = removeUser(socket.id);
if(user) {
io.to(user.room).emit('message', { user: 'Admin', text: `${user.name} has left.` });
io.to(user.room).emit('roomData', { room: user.room, users: getUsersInRoom(user.room)});
}
})
});
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/update-editor', (req, res) => {
pusher.trigger('editor', 'text-update', {
...req.body,
});
res.status(200).send('OK');
});
// Post request to compile the code
app.post('/editor', (req, res) => {
// hackerearth api secret key
var CLIENT_SECRET = "926a0a861df9fc10c5cd44d16d3b12cf1a0aef2c";
// data to be sent to the hackerearth api
var requ ={
url: "https://api.hackerearth.com/v3/code/run/",
method: 'POST',
form: {
'client_secret': CLIENT_SECRET,
'async': 0,
'source': req.body.source,
'lang': req.body.lang,
'time_limit': 5,
'memory_limit': 262144,
}
};
// sending request to the hackerearth api
request(requ, async (err, resp, body) => {
// Convert data to json
body = JSON.parse(body);
res.send(body);
});
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
app.set('port', process.env.PORT || 5000);
*/
/*
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});*/