0

我是这里的初学者。我目前正在使用 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'));
}
};
4

1 回答 1

0

在您的表单中,“操作”是应该发出请求的 url,例如action="/signup"

我假设你在你的例子中使用 express (来自res.render电话)。

使用 express 你需要将bodyParser中间件添加到你的中间件堆栈中: app.use(express.bodyParser());

在此之后,您需要一条发布路线: app.post('/signup', routes.postedSignup);

routes.postedSignup可能看起来像这样:

exports.postedSignup = 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: bcrypt.hashSync(req.body.password, 8),
            phone: req.body.phoneNumber || '555-555-555',
            birthday: new Date(req.body.birthday) || new Date()
        };
        db.userCollection.insert(obj, function (err) {
            if (!err) {
                res.redirect('/login');
            } else {
                next(err);
            }
        });
    } else {
        next(new Error('Incorrect POST'));
    }
};

这里db应该是您的数据库驱动程序,并且bcryptbcrypt模块,用于使用随机盐对密码进行哈希处理。

要从您的数据库中获取此数据,您将为它创建一个路由: app.get('/profile/:email', routes.getProfile);

然后这条路线看起来像这样(它假设用户已登录并且有一个有效的会话并且用户数据保存为req.session.user):

exports.getProfile = function (req, res, next) {
    if (req.session.user && req.session.user.email === req.params.email || req.session.user.admin) {
        db.userCollection.findOne({ email: req.params.email }, function (err, result) {
            if (!err && result) {
                res.locals.user = result;
                res.render('userprofile');
            } else {
                var errorMessage = err || new Error('no user found');
                next(errorMessage);
            }
        });
    } else {
        next(new Error('Insufficient permissions'));
    }
};

为了使数据res.locals.user可用,您需要使用某种模板将数据注入到res.render. 任何设置为res.locals(或作为对象传递给渲染调用)的值都可以在模板中使用,在这种情况下,您将访问的 var 将被调用user

这些next()调用将错误传递给错误处理程序,我假设您在中间件中作为最后一个错误处理程序。如果没有,你应该在这里阅读。

您还可以使用客户端模板来处理这种情况,因此您只会从路由中返回 json,而不呈现任何模板。为此,您只需将模板名称替换res.renderres.json您希望作为 json 发送的对象。

对于现实世界的应用程序,您必须进一步充实这一点,例如检查是否没有用户已经使用相同的电子邮件等。注册可以分为几个不同的功能,以便其他功能可以重用于编辑用户配置文件。

还包括express.csrf在您的中间件中以保护用户免受 csrf 攻击。

希望这会让你继续前进。

于 2013-05-24T07:30:47.283 回答