Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

message not showing in div #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
node_modules/
*.iml
.idea
20 changes: 19 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@ var app = express();
var logger = require("morgan");
var bodyParser = require("body-parser");
// Need to connect the socket to the http server
var server = require('http').createServer(app)
var port = process.env.PORT || 3000;
var router = express.Router();


app.set("views", __dirname + "/views");
app.use(logger('dev'));
app.engine('ejs', require('ejs').renderFile);
app.use(logger('dev'));
app.set('view engine', 'ejs');
app.use(express.static("public", __dirname + "/public"));
app.use(bodyParser.json());

// Write your code here

app.get('/', function(req, res) {
res.render('index');
})


server.listen(port);
console.log('Server started on ' + port);
var io = require('socket.io')(server);

io.on('connect', function(socket){
socket.on('send', function(data) {
socket.emit('message', data);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"dependencies": {
"body-parser": "1.4.3",
"ejs": "^2.3.4",
"express": "4.6.1",
"jade": "1.4.2",
"morgan": "^1.6.1",
Expand Down
17 changes: 16 additions & 1 deletion views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<link rel="stylesheet" type="text/css" href="/css/style.css">
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="/js/index.js"></script>
</head>
<body>
<div class="container-fluid">
Expand All @@ -26,6 +25,22 @@
</form>
<hr />
<div id="messages"></div>

<script type="text/javascript" src="/js/index.js">
var socket = io();

socket.on('connect', function() {
console.log('Connected!');
});

socket.on('message', function(data) {
var html = '';
for (var i = 0; i < messages.length; i++) {
html += messages[i] + '<br />';
}
$('#messages').append(html)
});
</script>
</div>
</div>
</body>
Expand Down