Skip to content

Instantly share code, notes, and snippets.

@kanakiyajay
Created February 25, 2014 16:30
Show Gist options
  • Save kanakiyajay/9212411 to your computer and use it in GitHub Desktop.
Save kanakiyajay/9212411 to your computer and use it in GitHub Desktop.
A simple way to use native form POSTs in express and myql
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");
<!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