is there a way to add CSS/JS later using EJS with nodejs/express

后端 未结 5 1319
情歌与酒
情歌与酒 2021-02-01 09:26

i\'m using the EJS template engine with nodejs/express and i\'m wondering if it\'s possible to add another css or js file in e.g the index.ejs (not the layout.ejs)

5条回答
  •  爱一瞬间的悲伤
    2021-02-01 10:25

    Thanks for illustrating this option pkyeck!

    In express 4.x app.locals is an object. Here's pkyeck's answer rewritten to work in express 4.x:

    app.locals.scripts = [];
    app.locals.addScripts=function (all) {
        app.locals.scripts = [];
        if (all != undefined) {
            return all.map(function(script) {
                return "";
            }).join('\n ');
        }
        else {
            return '';
        }
    };
    app.locals.getScripts = function(req, res) {
        return scripts;
    };

提交回复
热议问题