简体   繁体   English

Express.Static不适用于子目录

[英]Express.Static not working for subdirectory

Express.static is working great for any view in root directory, but it breaks when I add a subdirectory. Express.static适用于根目录中的任何视图,但是在添加子目录时会中断。

app.use(express.static(path.join(__dirname, 'public')));

Works: 作品:

//Homepage
router.get('/', isAuthenticated, function (req, res, next) {
    res.render('index.hbs', {user: req.user, message:req.flash('message')});      //add add'l data {title:'Express', addlData:'SomeData', layout:'layout'}
});

Doesn't Work: 不起作用:

//Organization Profile
router.get('/orgprofile/:username', function(req,res,next){
    User.findOne({username:req.params.username}, function(err,docs){
        res.render('orgprofile.hbs',{orgdetails:docs});
    });
});

I'm guessing __dirname is changed to whatever directory the get request is made from, but I just want to use the root (/public) directory for all static requests. 我猜__dirname更改为从哪个get请求发出的目录,但是我只想对所有静态请求使用根(/ public)目录。

Use it this way: 使用这种方式:

app.use('/', express.static(__dirname + '/public'));
app.use('/orgprofile', express.static(__dirname + '/public'));

Problem is not caused by express.static() expression. 问题不是由express.static()表达式引起的。 It might be due to wrong href value. 可能是由于href值错误。 express.static(): express.static():

app.use(express.static(__dirname + "/public"));

Reference: 参考:

<link rel="stylesheet" type="text/css" href="/stylesheets/main.css">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM