【问题标题】:Nodejs - multiple post requestsNodejs - 多个帖子请求
【发布时间】:2015-05-09 19:17:49
【问题描述】:

我使用 Nodejs 创建了一个小型快速服务器,我目前能够处理单个发布请求 - 检查用户是否存在。

我需要合并一个额外的发布请求,这将允许我注册一个新用户。注册请求来自一个单独的 HTML 页面,其中包括一个标准的注册表单。

鉴于我看到的帖子标题示例都是一样的:

app.post('/', function (req, res)

如何区分请求?

我的代码:

var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');

var connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '12345678',
    database: 'project_eclipse',
    port: 3306
});

connection.connect(function (err) {
    if (!err) {
        console.log("Database is connected ... \n\n");
    } else {
        console.log("Error connecting database ... \n\n");
    }
});

var app = express();


// instruct the app to use the `bodyParser()` middleware for all routes

app.use(bodyParser());
app.use(express.static(__dirname + '/public'));

app.post('/', function (request, response) {

    console.log('searching for user:  ', request.body.usr);
    //console.log(request.body.pass);
    var usr = request.body.usr;
    var pass = request.body.pass;

    connection.query('SELECT * FROM eclipse_users WHERE username=? AND password = md5(?)', [usr, pass], function (err, rows, fields) {

        if (!err) {
            //console.log('The solution is: ', rows);
            var n_rows = rows.length;
            console.log('number of rows returned: ', n_rows);
            if (n_rows == 1) response.json({
                msg: 'user exists'
            });
            else response.json({
                msg: 'user does not exist'
            });
        } else {
            console.log('Error while performing Query.');
            connection.end();
        }
    });
});

app.listen(80, "127.0.0.1");
console.log('Server running at http://127.0.0.1:80/');

【问题讨论】:

    标签: javascript html node.js


    【解决方案1】:

    变体1,另一个网址:

    app.post('/registration', function (req, res) {
        // ...
    });
    

    变体2,参数作用:

    app.post('/:action', function (req, res) {
        if (req.param('action') === 'registration') {
            // ...
        }
    });
    

    变体 3,通过帖子执行操作:

    app.post('/', function (req, res) {
        if (req.param('action') === 'registration') {
            // ...
        }
    });
    

    【讨论】:

    • 谢谢!你帮了我很多。
    【解决方案2】:

    一种方法是:

    app.post('/:register', function(request, response){
        console.log('registering user:  ',request.body.usr);
    }
    

    并在调用帖子时传递注册标志。

    但是,如果您使用 app.get,则检查用户有效性的代码会更好:

    app.get('/', function(...)) {...}
    

    这样你就可以有一个 app.post 没有注册部分的 register 变量。

    【讨论】:

    • 所以你的意思是把用户有效期改为 app.get('/', function(request, response) 并使用 app.post('/', function(request, response) 进行注册? 我会试试...
    • 是的。经验法则是:如果只读取数据,请使用 get。如果插入/更新数据,请使用 post。
    • 谢谢!你帮了我很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多