如何在express nodejs

时间:2016-09-16 14:49:42

标签: node.js express

我想为/ doc路径起诉中间件,以便它可以提供satic文件并具有基本身份验证。

但是当我尝试获取/ doc时,我无法获得/ doc错误。

有没有人有任何想法?

var app = express();
app.use(bodyParser.urlencoded({ extended: true }));    // parse application/x-www-form-urlencoded
app.use(bodyParser.json())  ;  // parse application/json
app.use(functio    //app.use('/doc', express.static('./doc'));n(req, res, next) {
    res.header("X-powered-by", "NodeJs");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization");
    next();
});
app.use(function(req, res, next) {
    console.log(' - ',req.originalUrl);
    next();
});

var router = express.Router();

router.use(express.basicAuth('testUser', 'testPass'));

router.get('/doc', function(req, res, next) {
    console.log("inside doc");
    express.static('./doc');
    next();
});

app.use('/conf',require('./v1/data_config'));

谢谢并尊重

1 个答案:

答案 0 :(得分:1)

2件事: 您必须将路由器添加到Express应用程序中,并且应该将“/ doc”路由移到路由器之外,因为身份验证可能会干扰您的应用程序。

var router = express.Router();
router.use(express.basicAuth('testUser', 'testPass'));
router.get('/', express.static('doc'));

app.use('/doc',router);