【问题标题】:Restify serveStatic and separate function call afterRestify serveStatic 和单独的函数调用之后
【发布时间】:2017-12-04 10:58:22
【问题描述】:

我正在尝试在用户输入路由“/”时提供静态文件。

server.get(/\/?.*/, [restify.serveStatic({ directory: 'public/', default: 'index.html'}), exampleFunction])

所以我的目标是服务器静态文件(它可以工作),然后运行一个名为 exampleFunction 的函数。

我怎样才能做到这一点?现在只调用了 serveStatic,之后我没有找到运行 exampleFunction 的方法。

【问题讨论】:

    标签: javascript node.js routing restify


    【解决方案1】:

    不知道你使用的是什么框架等,但我猜......

    您不能将数组传递给 server.get 但您可以创建自己的函数

    server.get(/\/?.*/, function(req, res){
        restify.serveStatic({ directory: 'public/', default: 'index.html'});
        customFunction();
    })
    

    【讨论】:

    • 我试过了,restify.serveStatic 不起作用,尽管 customFunction 被调用。
    【解决方案2】:

    已更新为

    server.get(
      "/",
      restify.plugins.serveStatic({
        directory: __dirname + "/frontend/assets",
        default: "frontend/index.html",
      })
    );
    

    【讨论】:

    • 我知道这是很久以前的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 2017-03-18
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多