authentication - Node.js Express Serve static files only with SIMPLE authentification -


i've problem since 2 days ago now. want authentification on static files. @ lot of post on internet , nothing answer question simply.

i try use basic-auth, has no effect on webpages.

i want simple dialog box before open static pages ask name , password , in server.js thing : if name == 'foo' , password =='pwd' "send static content".

but seems not simple might be.

there code :

/***************************************************************************/ //server node.js /** ************************************************************************ */  var express = require("express"); var app = express(); var bodyparser = require("body-parser");  // routers var router = express.router();  var mongoose = require("mongoose"); mongoose.connect('mongodb://localhost:27017/mydb'); var conn = mongoose.connection; var assert = require("assert");   var basicauth = require('basic-auth');  var auth = function (req, res, next) {   function unauthorized(res) {     res.set('www-authenticate', 'basic realm=authorization required');     return res.send(401);   };    var user = basicauth(req);    if (!user || !user.name || !user.pass) {     return unauthorized(res);   };    if (user.name === 'foo' && user.pass === 'bar') {     return next();   } else {     return unauthorized(res);   }; };  app.use('/', auth); app.use(express.static('public')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({"extended" : false}));  router.route("/data").get(function(req, res) { // non static route }); }).post(function(req, res) { // non static route });  router.route("/data/:id?").get(function(req, res) { // non static route });  router.route("/remove/:id?").get(function(req, res) { // non static route });  app.use('/', router);  app.listen(8080); console.log("listening port 8080"); 

thanks.

have nice day.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -