Created
February 25, 2014 16:30
-
-
Save kanakiyajay/9212411 to your computer and use it in GitHub Desktop.
A simple way to use native form POSTs in express and myql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mysql = require("mysql"), | |
express = require("express"), | |
app = express(); | |
var connection = mysql.createConnection({ | |
host : "localhost", | |
user : "root", | |
password : "", | |
database : "node" | |
}); | |
connection.connect(); | |
app.use(express.bodyParser()); | |
app.get("/",function (req,res) { | |
res.sendfile(__dirname+"/index.html"); | |
}); | |
app.post('/', function(req, res){ | |
console.log(req.body); | |
connection.query("INSERT into sample SET ?",req.body,function (err,result) { | |
if (err) throw err; | |
res.send("Created "+JSON.stringify(result)); | |
}); | |
}); | |
//connection.end(); | |
app.listen(3000); | |
console.log("listenening on 3000"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Mysql Editor</title> | |
</head> | |
<body> | |
<form method="post" action="/"> | |
<input type="text" name="first" placeholder="First-name"><br> | |
<input type="text" name="last" placeholder="Last-name"><br> | |
<input type="text" name="nick" placeholder="Nick-Name"><br> | |
<input type="number" name="extras" placeholder="Extras"><br> | |
<input type="submit" value="Submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment