Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make node.js and mysql work together?
    text
    copied!<p>Thanks in advance for your help!</p> <p>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. </p> <p>I know I'm supposed to use the node-mysql module (<a href="https://github.com/felixge/node-mysql" rel="nofollow">https://github.com/felixge/node-mysql</a>), but I must be doing it wrong.</p> <p>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?</p> <p>Here's my code. Can someone answer the questions above and tell me what I'm doing wrong below? Thanks!</p> <pre><code>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"); } }); </code></pre> <p>By the way, in package.json, I listed </p> <pre><code>"mysql": "2.0.0-rc2" </code></pre> <p>as a dependency and did </p> <pre><code>'npm install' </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload