使用把手节点模板设置img src时连接被拒绝

时间:2016-07-18 03:16:28

标签: node.js image templates handlebars.js src

我不熟悉在Node中使用模板功能。我正在使用把手模板引擎在节点服务器上呈现动态视图。我可以渲染动态html,但是无法为具有动态源的图像设置src属性。我从浏览器中收到连接拒绝错误:

GET uploads/CI_plot-II.png:1 GET http://localhost:8080/uploads/CI_plot-II.png net::ERR_CONNECTION_REFUSED

我从服务器上的静态文件夹中提供此图像,当我通过在浏览器中导航到它来获取它时,我可以看到它,所以我知道它就在那里并且活着。

相关的服务器代码如下所示:

var hbs = require('hbs');
...
app.engine('html', hbs.__express);
...
app.use(express.static('public'));
...
app.get("/manageResources", function(req, res, next){
  //get data about a yet-to-be approve resource and send it over to the client via the view
  db = createConnection()
  sql = "SQL QUERY ..."
  db.any(sql)
    .then(function(data){
      //parse the response
      nestedData = parseObjectDBResponse(data)
      localvars = {username: req.session.username, sessionID: req.session.id, resourceData: nestedData[0]}
      console.log(localvars)
      res.render('manageResources', localvars)
    }).catch(function(err){
      res.render("error", {error: err})
    })
})

客户端模板如下所示:

  <body>
    <div class='col-xs-12'>
      <h5>References</h5>
      {{#each resourceData.references}}
        <li style="list-style: none" class='hangingindent'>{{this.string}}</li>
      {{/each}}
    </div>
  <div id='image-holder' class='col-xs-6'>
    <img id='theImage' src={{resourceData.fileReference}}>
  </div>
  </body>
</html>

我尝试过一些解决方法,例如在图像上设置data属性,然后在使用jQuery加载页面后设置src,但这也不起作用。

我不确定发生了什么,甚至问题是在客户端还是服务器端。

更新

如果我刷新页面3或4次,有时图像会正确显示。

1 个答案:

答案 0 :(得分:0)

我发现问题是我在使用nodemon在文件更改时自动重启服务器。因为我使用的是FileStore会话存储,所以每次用户导航到网站中的页面时文件都会更改,因此服务器会自动重启并导致连接被拒绝错误。

使用常规节点修复问题。

相关问题