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 es 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/');
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 es 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/');
Share
Improve this question
edited Dec 8, 2016 at 17:52
cn0047
17.1k7 gold badges60 silver badges74 bronze badges
asked May 9, 2015 at 19:17
David FaizulaevDavid Faizulaev
5,76123 gold badges86 silver badges141 bronze badges
2 Answers
Reset to default 4Variant 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742326687a4422889.html
评论列表(0条)