我是这里的初学者。我目前正在使用 twitter bootstrap (html,css,js),node.js 用于服务器端,mongodb 用于 no-sql dbs。我一直在看一堆东西,正在寻找答案或任何方向,以了解如何处理表单,即在 html 端填写用户帐户注册(已经完成)
<code>
<form class="validateForm" id="registerform" method="POST" action="" accept-charset='UTF-8'>
<fieldset>
<legend>Register</legend> <br>
<input type="text" name="firstName" id="firstName" placeholder="First Name" maxlength="20" value=""/> <br>
<input type="text" name="lastName" id="lastName" placeholder="Last Name" maxlength="20" value=""/> <br>
<input type="text" name="email" id="email" placeholder="Email" maxlength="30" value=""/> <br>
<input type="password" name="password" id="password" placeholder="Password" value=""/> <br>
<input type="password" name="confirmPassword" id="confirmPassword"placeholder="Confirm Password" value=""/> <br>
<input type="text" name="phoneNumber" id="phoneNumber" placeholder="Phone Number" maxlength="10" value=""/> <br>
<input type="date" name="birthday" id="birthday" placeholder="Birthday" value=""/>
<br>
<label id="legalConfirm" for="agree"><input type="hidden" name="agree" value="0" />
<input type="checkbox" name="agree" id="agree" value="1" checked="checked" /> By clicking join you confirm that you accept our <a href="/privacy.html">Privacy Policy</a> and <a href="/terms.html">Terms of Service</a>.</label>
<input class="btn btn-primary" type="submit" name="create" value="Join"/>
<a href="/"><button type="button" class="btn">Cancel</button></a>
</fieldset>
</form>
那是我使用 twitter bootstrap 并在 js 端验证它的表单。但是,不确定要采取什么行动。
node.js 通过我的路由器为该页面提供服务。
<code>
exports.signUp = function(req, res){
res.render('signUp');
};
它位于 routes 文件夹中,在我的 server.js 文件中被称为 require('./routes/home.js) 。
我在 package.json 文件中有 mongoose 作为依赖项,并试图找到更多关于它的文档,这很令人困惑。
我的视图引擎使用的是 html(不是 jam 或 ejs),虽然我愿意使用其中任何一个,但它们很容易理解。
我直接从我的电脑上运行 mongo,但正在研究 mongohq 以使事情变得更容易一些。
基本上,如果有人可以帮助或指导我如何完成表格,以便可以将信息放入(POST)到 mongo 中的数据库中,然后从 mongo 中提取(GET)并作为用户配置文件提供到页面上。
我可能需要澄清的另一件事是如何将其显示在新页面上,例如我的个人资料页面(我是否必须创建模板/页面或如何链接为创建帐户而保存的信息到我的个人资料页面)。
使用 HTML、JS(jQuery、JSON)、Node.Js 和 Mongodb 指导从客户端到服务器端的整个表单流程,包括 POST(表单)和 GET(到新页面)。
更新
所以在这里我已经为 POST 尝试了这个,所以现在它正在工作,但是得到一个找不到 html 模块的错误。
这是我的 server.js 文件的代码。
<code>
var express = require('express')
, home = require('./routes/home.js')
, path = require('path')
, http = require('http')
, mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
app.post('/signUp', home.signUpUser);
---- 现在这是我的 home.js(又名路由器 js 文件)
<code>
var mongoose = require('mongoose');
var conn = mongoose.connection;
exports.index = function(req, res){
res.render('index');
};
exports.signUp = function(req, res){
res.render('signUp');
};
exports.about = function(req, res) {
res.render('about');
};
exports.signUpUser = function (req, res, next) {
if (req.body && req.body.email && req.body.password === req.body.confirmPassword) {
var obj = {
firstName: req.body.firstName || 'na',
lastName: req.body.lastName || 'na',
email: req.body.email,
password: req.body.password,
phone: req.body.phoneNumber || '555-555-555',
birthday: new Date(req.body.birthday) || new Date()
};
conn.collection('users').insert(obj, function (err) {
if (!err) {
res.redirect('/about.html');
} else {
next(err);
}
});
} else {
next(new Error('Incorrect POST'));
}
};