1

我想在自定义模板中提供带有 express/serve-index 的文件夹。

这是我尝试这样做的方法:

const express = require('express');
const app = express();
const exphbs = require('express-handlebars');
const serveIndex = require('serve-index');

app.use('/presentations', express.static('./presentations'), serveIndex('./presentations', {
        icons:true,
        stylesheet:'styles.css',
        view:'details',
        template: (locals,cb) => {
            cb(null,
                app.render('dir',{'title':'Presentations'},(err,html) => {
                    if(err) {console.log(err)};
                    console.log(html);
                    return html;
                })
            )
        }
  }));

正如您所料,这是它在尝试渲染此路线时返回的错误:

buffer.js:481
    throw new TypeError('"string" must be a string, Buffer, or ArrayBuffer');
    ^

    TypeError: "string" must be a string, Buffer, or ArrayBuffer
        at Function.byteLength (buffer.js:481:11)
        at send (/home/josselin/git/myRev/node_modules/serve-index/index.js:503:42)
        at /home/josselin/git/myRev/node_modules/serve-index/index.js:211:9
        at template (/home/josselin/git/myRev/index.js:32:9)
        at /home/josselin/git/myRev/node_modules/serve-index/index.js:209:7
        at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

我很确定我不应该那样使用app.render,但我真的不知道该怎么做......

在使用函数入口点之前,我尝试 app.render直接使用字符串返回,这样它至少会处理它并显示我的日志,并且在这种情况下html按应有的方式构造,但 serve-index 仍然使用其默认模板呈现。

作为参考,以下是我在 requires 中使用的版本:

"express": "4.16.2",
"express-handlebars": "3.0.0",
"serve-index": "1.9.1"
4

0 回答 0