简体   繁体   English

Express Router渲染和公共静态文件

[英]Express Router render and public static files

I have the following code in index.js : 我在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');
});

In my routes.js : 在我的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;

In my views/index.pug : 在我的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 +"')")

In my browser, if I access localhost:3000 , everything works correctly, I see a list of images from the subreddit earthporn . 在我的浏览器中,如果我访问localhost:3000 ,则一切正常,我看到了来自subreddit earthporn的图像列表。

However, if I access localhost:3000/subreddit/cute , for example, I get this in the logs: 但是,例如,如果我访问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

This is preventing me from rendering the images of the subreddit that the user enters in the URL. 这使我无法呈现用户在URL中输入的subreddit的图像。 I don't know why on earth is express receiving a request with subreddit/app.css , doesn't make any sense. 我不知道为什么地球上用subreddit/app.css表示接收请求,这没有任何意义。

In the network tab, in Chrome, when I enter localhost:3000/subreddit/cute I do see two requests: 在网络标签中的Chrome中,当我输入localhost:3000/subreddit/cute我确实看到了两个请求:

GET http://localhost:3000/subreddit/cute And right after GET http://localhost:3000/subreddit/app.css GET http://localhost:3000/subreddit/cuteGET http://localhost:3000/subreddit/app.css

So maybe this is a problem in chrome, I really don't understand. 所以也许这是Chrome中的问题,我真的不明白。 For the request localhost:3000 , everything works fine and just one request made. 对于请求localhost:3000 ,一切正常,仅发出了一个请求。

This line 这条线

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

is trying to request https://www.reddit.com '+ app.css +'.json' which is wrong so it is taking so long. 正在尝试请求https ://www.reddit.com'+ app.css +'。json',这是错误的,因此需要花费很长时间。

In index.pug try putting full url for app.css 在index.pug中,尝试将app.css的完整网址

Thanks for the answers. 感谢您的回答。 Fixed it by doing /app.css in my index.pug 通过在我的index.pug执行/app.css来修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM