简体   繁体   中英

how to set static path to 'public' folder for nested route in Nodejs

app.js

app.engine('.hbs', expressHbs({
        defaultLayout: 'layout', 
        extname: '.hbs',
        layoutsDir:'views/layouts',
        partialsDir:'views/partials'
}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', '.hbs');
app.use(express.static(path.join(__dirname, 'public')));

it work correctly in this route [admin.js]

router.get('/', function(req, res, next){
    res.render('admin/admin', {title: 'Admin'});
  });
});
//will render http://localhost:3000/admin

however, when i add new route in [admin.js]

router.get('/insert-scene', function(req, res, next){
    res.render('admin/insert-scene', {title: 'Insert Scene'});
});
//will render http://localhost:3000/admin/insert-scene

example:

<link href="./stylesheets/site.css" rel="stylesheet">    
http://localhost:3000/public/stylesheets/site.css[work][/admin] 
http://localhost:3000/admin/stylesheets/site.css[wrong path][/admin/insert-scene]

public folder not work in this route so hbs view just render /admin/.... in my ref source. How to solve this problem?

Do not serve static files with node, while node can handle it, it's not the intended use case of the technology.

Use a specific web server for that like nginx or a CDN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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