【发布时间】:2011-08-22 12:48:43
【问题描述】:
我正在尝试使用 NodeJS 编写一个 REST-API 服务器,就像 Joyent 使用的那样,一切都很好,除了我无法验证普通用户的身份验证。如果我跳转到终端并执行curl -u username:password localhost:8000 -X GET,我无法在 NodeJS http 服务器上获取值 username:password。如果我的 NodeJS http 服务器类似于
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
,我不应该在来自回调的 req 对象中的某处获取值 username:password 吗? 如何在不使用 Connect's basic http auth 的情况下获得这些值?
【问题讨论】:
-
console.dir(req.headers)
-
console.dir(req.headers) 仅输出 { 授权:'Basic am9hb2plcm9uaW1vOmJsYWJsYWJsYQ==','user-agent':'curl/7.21.3 (x86_64-pc-linux-gnu) libcurl /7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18',主机:'localhost:8000',接受:'/'}
-
Express 4 见this answer
标签: http authentication node.js basic-authentication