I am learning Node.js. I am trying to learn it without using 3rd party modules or frameworks.
我正在学习Node.js.我试图在不使用第三方模块或框架的情况下学习它。
I am trying to figure out how I can get the POST data from each individual input.
我试图弄清楚如何从每个输入中获取POST数据。
I am making a login system:
我正在建立一个登录系统:
<form method="POST" action="/login">
<input name="email" placeholder="Email Address" />
<input name="password" placeholder="Password" />
</form>
So the user logins and the input data is POSTed to the server.
因此用户登录和输入数据被POST到服务器。
The Node.js server is ready for this POST :
Node.js服务器已为此POST做好准备:
var http = require('http');
var server = http.createServer(function(req, res) {
if(req.method == POST && req.url == '/login') {
var body = "";
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
console.log(body);
});
}
});
server.listen(80);
The node.js server code above can console.log the body. For example, this will console.log the post data like this:
email=emailaddy@gmail.com password=thisismypw
上面的node.js服务器代码可以在console.log中使用。例如,这将是console.log这样的帖子数据:email = emailaddy@gmail.com password = thisismypw
BUT how do I get the input data individually? I think in PHP I could target each input this way:
但我如何单独获取输入数据?我认为在PHP中我可以通过这种方式定位每个输入:
$_POST['email'] and $_POST['password']
and I could put these values into a variable and use them to INSERT or CHECK the database.
我可以将这些值放入变量并使用它们来INSERT或CHECK数据库。
Can someone please show me how I can do this in Node.js? Please no modules barebones please!
有人可以告诉我如何在Node.js中做到这一点吗?请不要模块准系统!
1 个解决方案
#1
9
For application/x-www-form-urlencoded
forms (the default) you can typically just use the querystring
parser on the data:
对于application / x-www-form-urlencoded表单(默认),通常只需对数据使用查询字符串解析器:
var http = require('http'),
qs = require('querystring');
var server = http.createServer(function(req, res) {
if (req.method === 'POST' && req.url === '/login') {
var body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
var data = qs.parse(body);
// now you can access `data.email` and `data.password`
res.writeHead(200);
res.end(JSON.stringify(data));
});
} else {
res.writeHead(404);
res.end();
}
});
server.listen(80);
For multipart/form-data
forms you're going to be better off using a third party module because parsing those kinds of requests are much more difficult to get right.
对于多部分/表单数据表单,您最好使用第三方模块,因为解析这些类型的请求要难得多。
#1
9
For application/x-www-form-urlencoded
forms (the default) you can typically just use the querystring
parser on the data:
对于application / x-www-form-urlencoded表单(默认),通常只需对数据使用查询字符串解析器:
var http = require('http'),
qs = require('querystring');
var server = http.createServer(function(req, res) {
if (req.method === 'POST' && req.url === '/login') {
var body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
var data = qs.parse(body);
// now you can access `data.email` and `data.password`
res.writeHead(200);
res.end(JSON.stringify(data));
});
} else {
res.writeHead(404);
res.end();
}
});
server.listen(80);
For multipart/form-data
forms you're going to be better off using a third party module because parsing those kinds of requests are much more difficult to get right.
对于多部分/表单数据表单,您最好使用第三方模块,因为解析这些类型的请求要难得多。