New to node, confused about app.js

45 views
Skip to first unread message

TT

unread,
Feb 10, 2015, 11:46:35 PM2/10/15
to nod...@googlegroups.com
Hi. I am following tutorials and learning a lot but no one has really broken down app.js to a newbie.

So far with success I have my server running, I've installed Express and socket.io and made a little chat page that works. I did this by adding the following to app.js

var http    = require('http').Server(app);
var io      = require('socket.io')(http);

After the default 404 error handlers created by Express I add

io.on('connection', function(socket){
 socket
.on('disconnect', function(){
 
});
 socket
.on('chat message', function(msg){
 io
.emit('chat message', msg);
 
});
});

http
.listen(3000, function(){
 console
.log('listening on *:3000');
});

Finally I created a chat.js in public/javascripts/ so the user can chat.


Question #1: Should I be initializing the socket in app.js? What if I have some static page that doesn't need any web socket stuff?



Question #2: I am trying the same thing with node-myqsl, In app.js:

var mysql = require('mysql');

But the only way I can get queries to work is to place them all in app.js. That doesn't seem proper. Where do I put these additional modules? Where do I store all my prepared queries?

var connection = mysql.createConnection({
 host
: 'localhost',
 user
: 'root',
 password
: 'admin'
});

connection
.query('SELECT email FROM user.users', function(err, rows, fields) {
 
if (err) throw err;
});

connection
.end();

Thanks for any help explaining!

Bruno Jouhier

unread,
Feb 11, 2015, 1:55:32 AM2/11/15
to nod...@googlegroups.com
connection.end is called before your query completes. Try:

connection.query('SELECT email FROM user.users', function(err, rows, fields) {
 
if (err) throw err;
connection.end();
});
Reply all
Reply to author
Forward
0 new messages