我想製作一個註冊表格。按下「提交」按鈕後,腳本將打開我的MySql數據庫,並在「用戶」表中插入新行。 一切正常,但瀏覽器無法解釋NodeJs require
函數。 一些其他職位據說使用browserify,但我想知道如果這可以做到沒有其他程序。用NodeJs在MySql中保存用戶信息
我的代碼:
register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script src = "index.js" type="text/javascript"></script>
<script src = "register.js" type = "text/javascript"></script>
<link rel="stylesheet" type="text/css" href = "register.css">
</head>
<body>
<form name = "registerForm" method = "post">
<p id = "fillRule">Username must contain lower or uppercase or _! Fill in all boxes except the job! Pass must contain small and uppercase letters and its lenght must be at least 6 char, max 10!</p>
<label for="username">Username:</label>
<input type = "text" id = "username" name = "username" onchange = "checkUser()"> <br/>
<label for="firstname">Firstname:</label>
<input type = "text" id = "firstname" name = "firstame" onchange = "checkFirst()"> <br/>
<label for="lastname">Lastname:</label>
<input type = "text" id = "lastname" name = "lastname" onchange = "checkLast()"> <br/>
<label for="job">Your job:</label>
<input type = "text" id = "job" name = "job" onchange="checkJob()"> <br/>
<label for="pass">Password:</label>
<input type = "text" id = "pass" name = "pass" onchange = "checkPass()"> <br/>
<label for="mail">E-mail:</label>
<input type = "email" id = "mail" name = "mail" onchange = "checkMail()"> <br/>
<input type = "button" value = "send" id = "gomb" disabled = "disabled" onclick = "registerButtonClick()">
</form>
</body>
</html>
index.js
//export {addUser};
// String formatting
if (!String.format) {
String.format = function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
//
var mysql = require("mysql");
function createConnection(hostName, userName, passwd, db)
{
const con = mysql.createConnection({
host: hostName,
user: userName,
password: passwd,
database: db
});
return con;
}
function openConnection(con)
{
con.connect(function (err) {
if (err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
}
function executeFullSelect(con, tableName)
{
const statement = "SELECT * FROM " + tableName + ";";
con.query(statement, function(err, rows)
{
if (err)
{
throw err;
}
console.log("Data received from " + tableName + ".\n");
console.log(rows);
/*for (var i = 0; i < rows.length; i++)
{
console.log(rows[i].UserName);
}*/
});
}
function insert(con, userName,firstName, lastName, job, mail, passwd)
{
const statement = String.format("call newUser(\"{0}\", \"{1}\", \"{2}\", \"{3}\", \"{4}\", \"{5}\");", userName, firstName, lastName, job, mail, passwd);
con.query(statement, function(err, rows)
{
if (err)
{
console.log("Error at insert");
return 0;
}
console.log(userName + " was successfully added:)");
return 1;
})
}
function closeConnection(con)
{
con.end(function (err) {});
}
function addUser(userName, firstName, lastName, job, mail ,passwd)
{
const con = createConnection("localhost", "root", "micimacko", "users");
openConnection(con);
if (!insert(con, userName, firstName, lastName, job, mail ,passwd))
{
closeConnection(con);
return 0;
}
closeConnection(con);
return 1;
}
register.js
function registerButtonClick()
{
const userName = document.forms["registerForm"]["username"].value;
const firstName = document.forms["registerForm"]["firstname"].value;
const lastName = document.forms["registerForm"]["lastname"].value;
const job = document.forms["registerForm"]["job"].value;
const mail = document.forms["registerForm"]["mail"].value;
const pass = document.forms["registerForm"]["pass"].value;
if (addUser(userName, firstName, lastName, job, mail, pass))
{
alert("added");
}
else
{
alert("failed");
}
}
爲什麼在網頁上包含服務器端代碼? –
那是我的問題。我應該怎麼做纔對? –
通過在node.js中運行node.js代碼,而不是試圖將其包含在網頁上。他們是完全獨立的東西。您確實已將node.js設置爲服務器? –