Front page code:
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> ADD EMP </title>
<body>
<form id= "Form1" action= "index.html" method= "POST" >
User name: <input type= "text" name= "name" value= "" ><br/>
Password: <input type= "password" name= "pswd" value= "" ><br/>
<input type= "Submit" value= "Submission"/>
</form>
</body>
Background Nodejs Code:
' Use strict ';
var express=require (' Express ');
var http=require (' http ');
var fs=require (' FS ');
var querystring=require (' QueryString ');
var app=express ();
var mysql=require (' MySQL ');
var Pool=mysql.createpool ({
Host: ' 127.0.0.1 ',
Port: ' 3306 ',
Database: ' Test ',
User: ' Root ',
Password: ' 12345678 ',
});
App.get ('/index.html ', function (REQ,RSP) {
Rsp.writehead (200,{' content-type ': ' text/html ');
Fs.createreadstream (' index.html '). Pipe (RSP);
});
App.post ('/index.html ', function (req,res) {
Req.on (' Data ', function (data) {
var obj=querystring.parse (data.tostring ());//Get page parameters
Pool.getconnection (function (err,connection) {
if (err) {
Res.send (' Can not connect to MYSQL DB ');
Console.log (ERR);
}else{
var str;
Connection.query (' INSERT INTO EMP set? ', {name:obj.name,password:obj.pswd},function (Err,result) {
if (err) {
Str= ' Insert failed ';
}else{
Str= "Insert succeed";
}
connection.release(); res.send(str); }) } });});
});
App.listen (8080, "127.0.0.1");
Insert Effect:
Interpolate to Database