Thanks in advance for your help!
I am making a node/express app and I want to use a mysql database. But I can't seem to connect to the database.
I know I'm supposed to use the node-mysql module (https://github.com/felixge/node-mysql), but I must be doing it wrong.
I'm completely new to this. Do I have to create a database and then create a table? Is there a way to create a database elsewhere so it doesn't get erased every time I restart the app?
Here's my code. Can someone answer the questions above and tell me what I'm doing wrong below? Thanks!
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
io = require('socket.io'),
path = require('path');
var app = express();
var server = http.createServer(app);
sio = io.listen(server);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var mysql = require('mysql');
var connection = mysql.createConnection({
user : 'me',
password : 'secret',
host : 'localhost',
port : app.get('port')
});
connection.connect();
connection.query('CREATE TABLE tesTable (integer int, textfield VARCHAR(100), PRIMARY KEY(integer))',
function(err, result){
if(err) {
console.log(err);
} else {
console.log("Table testTable Created");
}
});
By the way, in package.json, I listed
"mysql": "2.0.0-rc2"
as a dependency and did
'npm install'