Express Router渲染和公共静态文件

时间:2016-12-01 13:18:09

标签: javascript google-chrome express

我在index.js中有以下代码:

var express = require('express');
var app     = express();
var PORT    = 3000;
var routes = require('./scripts/routes/routes');

app.set('views', './views');

app.set('view engine', 'pug');

app.use(express.static('public'));

app.use('/', routes);

app.listen(PORT, function() {
  console.log('App running');
});

在我的routes.js

var express = require('express');
var router = express.Router();
var request = require('request');

router.get('/', function (req, res) {
  request('https://www.reddit.com/r/earthporn.json', function(error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      var items = data['data']['children'];
      var urls = items.map(function(item) {
        item_data = item['data'];
        item_preview = item_data['preview'];
        item_images = item_preview['images'];
        return item_images.map(function(image) {
          return image['source']['url'];
        });
      });

      res.render('index', { subReddit: 'earthporn', images: urls });
    }
  });
});

router.get('/subreddit/:subreddit', function (req, res) {
  var subreddit = '/r/' + req.params['subreddit'];
  request('https://www.reddit.com'+ subreddit +'.json', function(error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      var items = data['data']['children'];
      var urls = items.map(function(item) {
        item_data = item['data'];
        item_preview = item_data['preview'];
        item_images = item_preview['images'];
        return item_images.map(function(image) {
          return image['source']['url'];
        });
      });

      res.render('index', { subReddit: subreddit, images: urls });
    }
  });
});

module.exports = router;

在我的views/index.pug

html
  head
    link(href='app.css' rel="stylesheet")
    title Hello World!
  body
    div(class="container")
      h1= subReddit

      div(class="images_gird")
        each url in images
          div(class="image", style="background-image: url('"+ url +"')")

在我的浏览器中,如果我访问localhost:3000,一切正常,我会看到来自subreddit earthporn的图片列表。

但是,例如,如果我访问localhost:3000/subreddit/cute,我会在日志中看到这个:

express:router dispatching GET /subreddit/cute +2s
  express:router query  : /subreddit/cute +1ms
  express:router expressInit  : /subreddit/cute +1ms
  express:router serveStatic  : /subreddit/cute +0ms
  express:router router  : /subreddit/cute +5ms
  express:router dispatching GET /subreddit/cute +0ms
  express:view lookup "index.pug" +4s
  express:view stat ".../views/index.pug" +0ms
  express:view render ".../views/index.pug" +0ms
  express:router dispatching GET /subreddit/app.css +83ms !! <<--- Why is this happening?
  express:router query  : /subreddit/app.css +0ms
  express:router expressInit  : /subreddit/app.css +1ms
  express:router serveStatic  : /subreddit/app.css +0ms
  express:router router  : /subreddit/app.css +0ms
  express:router dispatching GET /subreddit/app.css +1ms

这使我无法呈现用户在URL中输入的subreddit图像。我不知道为什么在地球上明确表示收到subreddit/app.css的请求,没有任何意义。

在网络标签中,在Chrome中,当我输入localhost:3000/subreddit/cute时,我确实看到了两个请求:

GET http://localhost:3000/subreddit/cute 紧接着 GET http://localhost:3000/subreddit/app.css

所以也许这是chrome中的一个问题,我真的不明白。对于请求localhost:3000,一切正常,只做了一个请求。

2 个答案:

答案 0 :(得分:0)

这一行

express:router dispatching GET /subreddit/app.css +83ms

正在尝试请求https://www.reddit.com'+ app.css +'。json' 这是错误的,所以花了这么长时间。

在index.pug中尝试为app.css输入完整的URL

答案 1 :(得分:0)

感谢您的回答。通过在/app.css

中执行index.pug来解决此问题