简体   繁体   中英

Nodejs - multiple post requests

I've created a small express server using Nodejs and I'm currently able to handle a single post request - to check if a user exists or not.

I need to incorporate an additional post request, which would allow me to register a new user. The registration request comes from a separate HTML page which includes a standard registration form.

Given that the post header examples I've seen are all the same:

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

How I can distinguish between the requests?

My code:

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/');

Variant 1, another url:

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

Variant 2, parameter action:

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

Variant 3, action through post:

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

One way to do this is:

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

And pass the register flag while calling the post.

However, your code to check the validity of the user is better off if you use app.get:

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

This way you can have an app.post without the register variable for the registration part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM