相关疑难解决方法(0)

Node.JS中的基本HTTP身份验证?

我正在尝试使用类似于Joyent使用的NodeJS编写REST-API服务器,除了我无法验证普通用户的身份验证之外,一切正常.如果我跳到终端并且这样做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");
Run Code Online (Sandbox Code Playgroud)

,我不应该在来自回调的req对象中获取值username:password 吗?如何在不使用Connect的基本http auth的情况下获取这些值?

authentication http basic-authentication node.js

45
推荐指数
5
解决办法
6万
查看次数

express.basicAuth抛出错误

我刚刚使用express basic auth在nodejs中创建了基本身份验证

var express = require('express');
var app = express();

// Authenticator
app.use(express.basicAuth('testuser','testpassword'));

app.get('/home', function(req, res) {
 res.send('Hello World');
});

app.listen(process.env.PORT || 8080);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.我不知道我哪里出错了.

app.use(express.basicAuth('testuser','testpassword'));
                ^
TypeError: Object function createApplication() {
  var app = function(req, res, next) {
    app.handle(req, res, next);
  };

  mixin(app, proto);
  mixin(app, EventEmitter.prototype);

  app.request = { __proto__: req, app: app };
  app.response = { __proto__: res, app: app };
  app.init();
  return app;
} has no method 'basicAuth'
    at Object.<anonymous> (E:\node_modules\npm\login\app.js:5:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10) …
Run Code Online (Sandbox Code Playgroud)

basic-authentication node.js

11
推荐指数
1
解决办法
6736
查看次数