Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在express nodejs中为子路径使用中间件路由器_Node.js_Express - Fatal编程技术网

Node.js 如何在express nodejs中为子路径使用中间件路由器

Node.js 如何在express nodejs中为子路径使用中间件路由器,node.js,express,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/j

我想为/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'));
感谢并关心两件事: 您必须将路由器添加到express应用程序中,并且应将“/doc”路由移到路由器之外,因为身份验证可能会干扰您的应用程序

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

app.use('/doc',router);
你是否有
app.use(“/”,路由器)在该文件的末尾?