【问题标题】:Serving a mix of static and dynamic content with node.js & express使用 node.js 和 express 提供静态和动态内容的混合
【发布时间】:2014-08-05 07:07:34
【问题描述】:

这类似于条目21105801,但不完全一样。

(版本:express@4.7.2、node@0.10.29)

我正在尝试创建一个路由,如果找到它,它将为动态页面提供服务,如果没有,则传递给静态路由器(通过 next())。

我的路由应该是这样操作的,app.get('/' -> authenticate -> try to serve dynamic -> try to serve static.

我添加了一个处理 Oauth 1 和 2 的授权路由,仅使用静态页面就一切正常。但是我想在身份验证后提供自定义页面,并且我遇到了三个问题。

  1. 我不想使用“render()”按名称显式服务每个动态页面,因此我手动构建文件路径并绕过“视图”逻辑。这适用于 HTML,但是...
  2. 它不适用于我用于我的 Foundations 的 JS 和 CSS。其实render()从来不会因为找不到css/js文件而调用回调,甚至认为路径是模块?
  3. 视图引擎要求所有内容都在一个视图路径中,这似乎很奇怪。还是我遗漏了一个明显的设计模式?

路线如下:

app.use(
    "/", // i like being pedantic
    authorize, // pass through the auth filter
    function(req, res, next) { // try to serve dynamic page
        // build the filename
        var file = __dirname + '/restricted' + req.url;
        console.log("Rendering file "  + file);
        res.render(file, { username: req.user.username } , function(err, html) {
            if (err) {
                console.log("ERROR: " + err);
                next(); // pass to static router
            } else {
                res.send(html); // or send the page
            }
        })
    },
    // fallthrough route for js/css statics
    express.static(__dirname + "/restricted")
);

这是错误,它正在尝试读取调用 CSS 模板和 JS rel 的 index.html。

GET /auth/twitter/callback?oauth_token=... 302 329.979 ms - 78
Rendering file /home/pt/www/restricted/index.html
ERROR: null
       ^---- this worked!
GET /index.html 200 7.097 ms - 838
Rendering file /home/pt/www/restricted/css/foundation.css
               ^---- that's the file I want to read!
GET /css/foundation.css 500 9.846 ms - 871
    ^---- keep going!!!
Error: Cannot find module 'css'
                   ^---- DOH! Why does it look for a module?!?!? Why no error callback?
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)

提前致谢。

我想我正在尝试做一些非常明显的事情,但几乎所有的动态视图引擎示例都为每个显式文件调用 res.render('filename') 这似乎很荒谬:为什么每次添加时都更改路由器代码新的动态页面?

【问题讨论】:

  • 你确定你可以像这样将多个函数传递给 app.use() 吗?他们的网站现在不适合我,所以我正在查看这些文档:web.archive.org/web/20140706041538/http://expressjs.com/api
  • @mshindal :不,这不是这里的问题。他可以根据需要使用app.use() 中的任意数量的功能。 @PeterT,您真的需要在您的静态内容之前 尝试提供动态内容吗?通常人们会反其道而行之。
  • 嗨@WaldoJeffers:不,我不需要在动态之前提供静态,但是如果我交换上面的规则,静态路由器不会在动态之前提供 index.html 的模板版本解析器渲染它? (我可以将我的静态和动态内容分开,但它们都使用相同的 js/css 子目录,存储多个副本感觉很奇怪。)
  • 通常,一个应用看起来像:app.use("/public", modules.express.static('./public'));app.get("/index, function(req,res){ res.render("index.jade"); });app.get("/faq", function(req,res){ res.render("faq.jade");});
  • 我不知道它是否“皱眉”,但绝对不是通常的方式。实际上,这些应用程序通常具有固定数量的端点(意味着您每次都编辑路由器)。如果你真的想创建动态路由,我建议你这样做:app.get("/dynamic/:page",function(req,res){ res.render(req.params["page"] + ".jade"); });

标签: node.js dynamic express static


【解决方案1】:
  • 对静态内容使用全局通配符路由[app.use('/')] 和
  • 使用特定路由 [app.get(/myroute), app.post('/anotherroute')] 使用自定义逻辑进行动态处理
//Serves resources from public folder 
app.use('/',express.static(__dirname + '/public')); 

//Verify the complete directory path - especially slashes 
console.log('Static directory '+__dirname + '/public');

//serve on custom routes
app.get('/list', function (req, res) {
    res.send('<html><body><h1>Hello World</h1></body></html>'); });

【讨论】:

    猜你喜欢
    • 2016-09-14
    • 2011-09-01
    • 1970-01-01
    • 2016-02-25
    • 2015-02-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多