-
-
Save clarle/3180770 to your computer and use it in GitHub Desktop.
// Module dependencies | |
var express = require('express'), | |
mysql = require('mysql'); | |
// Application initialization | |
var connection = mysql.createConnection({ | |
host : 'localhost', | |
user : 'root', | |
password : 'password' | |
}); | |
var app = module.exports = express.createServer(); | |
// Database setup | |
connection.query('CREATE DATABASE IF NOT EXISTS test', function (err) { | |
if (err) throw err; | |
connection.query('USE test', function (err) { | |
if (err) throw err; | |
connection.query('CREATE TABLE IF NOT EXISTS users(' | |
+ 'id INT NOT NULL AUTO_INCREMENT,' | |
+ 'PRIMARY KEY(id),' | |
+ 'name VARCHAR(30)' | |
+ ')', function (err) { | |
if (err) throw err; | |
}); | |
}); | |
}); | |
// Configuration | |
app.use(express.bodyParser()); | |
// Main route sends our HTML file | |
app.get('/', function(req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
// Update MySQL database | |
app.post('/users', function (req, res) { | |
connection.query('INSERT INTO users SET ?', req.body, | |
function (err, result) { | |
if (err) throw err; | |
res.send('User added to database with ID: ' + result.insertId); | |
} | |
); | |
}); | |
// Begin listening | |
app.listen(3000); | |
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); |
<!doctype html> | |
<html> | |
<head> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
$('#user-submit').click(function () { | |
var payload = { | |
name: $('#user-name').val() | |
}; | |
$.ajax({ | |
url: "/users", | |
type: "POST", | |
contentType: "application/json", | |
processData: false, | |
data: JSON.stringify(payload), | |
complete: function (data) { | |
$('#output').html(data.responseText); | |
} | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<h3>Enter a username to enter into the database:</h3> | |
<input id="user-name" type="text" /> | |
<input id="user-submit" type="submit" /> | |
<p id="output"></p> | |
</body> | |
</html> |
{ | |
"name": "reddit-node-mysql" | |
, "description": "A demo of how to use Express and MySQL together" | |
, "author": "Clarence Leung <github@clarle>" | |
, "version": "0.0.1" | |
, "private": true | |
, "dependencies": { | |
"express": "~2.5", | |
"mysql": "~2.0" | |
} | |
} |
Its not working for me, So I just change
res.sendfile( '/index.html' , {root:__dirname}); //instead of res.sendfile( __dirname + '/index.html' );
How do you pass an object to the view in a regular page load? NOT an ajax call. That object (of course) came from the mysql database. I'd imagine its something like this:
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html', {fromdb: 'list of records'});
});
let me know how to do this. thanks.
I get this error:
jquery.min.js:4 XMLHttpRequest cannot load file:///C:/api/users/insert. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
How do I fix it?
Thanks,
But i want to bind data from mysql to html page.
Thanks nice demo!
nice demo for we bigginers
Sorry im starting out with node so pardon me!..
Don't you think there need to be statement which closes the connection like connection.end()
Thanks for this 👍 And ohadpartuck if you wanted to move connection into a separate file you could just place it in, say, connection.js and in that file
module.exports = connection
thenconnection = require('./connection.js')
in your main app.js file.