-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
64 lines (51 loc) · 1.71 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
/**
* NodeJS entry file to initialize the App
* @author dassiorleando
*/
var express = require('express'),
path = require('path'),
http = require('http'),
cors = require('cors'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
gistAPI = require('./server/resources/gist'), // Get gists resource
app = express();
// Simple database connection
var dbURI = process.env.MONGODB_URI || 'mongodb://localhost/gistology';
mongoose.connect(dbURI, { useNewUrlParser: true });
// Enable CORS for all routes
app.use(cors());
// Create the HTTP server
var server = http.createServer(app);
// Socket.io for real time communication
var io = require('socket.io').listen(server);
// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Point static path to dist folder (dist is resulting from 'ng build')
app.use(express.static(path.join(__dirname, 'dist')));
// Set our api routes
app.use('/api/gists', gistAPI);
// Catch all other routes and return the index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
// Use either the port from the env or 3000
const port = process.env.PORT || '3000';
app.set('port', port);
// Listen on provided port, on all network interfaces.
server.listen(port, () => console.log(`API running on localhost:${port}`));
/**
* Socket events
*/
io.sockets.on('connection', function(socket) {
console.log('Socket connected');
// Socket event for gist created
socket.on('gistSaved', function(gistSaved) {
io.emit('gistSaved', gistSaved);
});
// Socket event for gist updated
socket.on('gistUpdated', function(gistUpdated) {
io.emit('gistUpdated', gistUpdated);
});
});