admin管理员组文章数量:1332345
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.
本文标签: javascriptNodejsmultiple post requestsStack Overflow
版权声明:本文标题:javascript - Nodejs - multiple post requests - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742326686a2453864.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论