basic-auth在节点server.js中不起作用。怎么修?

时间:2019-04-27 18:18:48

标签: reactjs express mern

在此示例中,我的基本身份验证不适用于节点server.js。 版本号 谁能指出我做错了什么? package.json具有“ express”:“ ^ 4.14.0”, “ basic-auth”:“ ^ 2.0.1”, 修改了这个原始项目:https://github.com/radhika-sivarajan/Quotes-MERN

// Require our dependecies
var express = require("express");
var mongoose = require("mongoose");
var bluebird = require("bluebird");
var bodyParser = require("body-parser");
var routes = require("./routes/routes");
// Set up a default port, configure mongoose, configure our middleware
var PORT = process.env.PORT || 3000;
mongoose.Promise = bluebird;
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public"));
//app.use("/", routes);


var db="mongodb://heroku1:heroku1@ds145786.mlab.com:45786/heroku_4wrjr2ks"


var auth = require('basic-auth');


var authFunc = function(req, res, next){
  var user = auth(req);
    //console.log("In authFunc ............." + user);
  if(user && user.pass == '****'){ // Here you need some logic to validate authentication
    next();
  } else {
    res.set({
      'WWW-Authenticate': 'Basic realm="simple-admin"'
    }).send(401);
  }
};



app.use("/", authFunc);
app.get("/", authFunc, function (req, res) {
    var a =1; //authenticated
});

    app.use('/', routes);


// Connect mongoose to our database
mongoose.connect(db, function(error) {
  // Log any errors connecting with mongoose
  if (error) {
    console.error(error);
  }
  // Or log a success message
  else {
    console.log("mongoose db connection is successful");
  }
});

// Start the server
app.listen(PORT, function() {
  console.log("Now listening on port %s! Visit localhost:%s in your browser.", PORT, PORT);
});


not showing user/password basic dialog.
works without authenticating

0 个答案:

没有答案