Created
December 26, 2013 10:47
-
-
Save myrtleTree33/8132188 to your computer and use it in GitHub Desktop.
Express server with socket.io, up and running Both Client and Server run on NodeJS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################### | |
# IMPORTANT. WORKS ON: | |
# Express >= v3.x | |
# Socket.io >= v1.0 | |
###################### | |
io = require('socket.io-client') | |
socket = io('http://localhost:3000') | |
socket.on 'news', -> | |
console.log 'hi' | |
return | |
socket.on 'pong', (data) -> | |
console.log 'PONG', data | |
return | |
setInterval(-> | |
socket.emit 'ping', 77 | |
return | |
, 1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
# Express App on port DEV_PORT (for me 9000), Socket.io app on port 3000 | |
# With traditional MVC framework, adapted from Yeoman generator, angular-fullstack. | |
####################### | |
# IMPORTANT. WORKS ON: | |
# Express >= v3.x | |
# Socket.io >= v1.0 | |
###################### | |
# Module dependencies. | |
express = require("express") | |
path = require("path") | |
fs = require("fs") | |
app = express() | |
server = require('http').Server(app) | |
io = require('socket.io')(server) | |
server.listen(3000) | |
# Connect to database | |
db = require("./lib/db/mongo") | |
# Bootstrap models | |
modelsPath = path.join(__dirname, "lib/models") | |
fs.readdirSync(modelsPath).forEach (file) -> | |
require modelsPath + "/" + file if path.extname(file) is '.js' | |
# Populate empty DB with dummy data | |
require "./lib/db/dummydata" | |
# Express Configuration | |
require("./lib/config/express") app | |
# Controllers | |
api = require("./lib/controllers/api") | |
index = require("./lib/controllers") | |
# Server Routes | |
app.get "/api/awesomeThings", api.awesomeThings | |
# Angular Routes | |
app.get "/partials/*", index.partials | |
app.get "/*", index.index | |
io.on 'connection', (socket) -> | |
console.log('i got connected') | |
socket.on 'ping', (value) -> | |
socket.emit 'pong', 78 | |
return | |
# Start server | |
port = process.env.PORT or 3000 | |
app.listen port, -> | |
console.log "Express server listening on port %d in %s mode", port, app.get("env") | |
# Expose app | |
exports = module.exports = app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment