【问题标题】:displaying images in a .handlebars/html在 .handlebars/html 中显示图像
【发布时间】:2016-06-19 08:33:12
【问题描述】:

我正在尝试将图像链接到我的视图目录中的 .handlebars/html 文件。我已经创建了一个公用文件夹,因为我发现这是必要的,但是当我打开我的网页时仍然无法显示链接的图像。

这是我的 node.js 代码...

var express = require('express');
var session = require('express-session');
var request = require('request');

var app = express();
var handlebars = require('express-handlebars').create({defaultLayout: 'main'});
var bodyParser = require('body-parser');

app.use(session({secret:'secretSauce'}));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 3021);

app.get('/', function (req, res, next) {
res.render('index');
});

app.get('/setup', function (req, res, next) {
    res.render('setup')
});

app.use(function(req,res){
  res.status(404);
  res.render('404');
});

app.use(function(err, req, res, next){
  console.error(err.stack);
  res.type('plain/text');
  res.status(500);
  res.render('500');
});

app.listen(app.get('port'), function(){
  console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
});

这是我尝试加载图片的页面:

<h1>
Getting a Mailjet Account, an API, and Misc. Set Up
</h1><br>
<p>
This first part is going to their website and signing up to get an account. The sign up page looks like this. You can click on the image to take you there.
</p>
<a href="https://app.mailjet.com/signup"><img src="/public/images/mailjet signup.jpg" alt="sign up page"></a>
<br>
<p>
Once you take care of business there, you can head to your account page and click on the link circled in the image below. That link will take you to where your private and public API keys are stored.
</p>
<img src="/public/images/mailjet APIKey.jpg" alt="account page">
<br>
<p>
Awesome, so the last major thing to think about is if you want to add a domain name to your account. Typically your emails that you use at sign up will be autamically set up as a sender, and it will make it look like emails are coming from that account. However, you may have multiple senders on a company domain. In that case you'll want to head over to the accuont settings and add that domain. This way in the future if employees send something it will automatically allow senders from the domain. This is really more of a logistical matter than     anything, and it doesn't directly affect using this How to Guide.
</p>
<ul>
  <li><a href="/">Prev</a> </li>
  <li><a href="">Next</a></li>
</ul>

【问题讨论】:

  • 你的img src应该有空格吗?例如&lt;img src="/public/images/mailjet signup.jpg",如果它位于/public/images/mailjet/ 文件夹中,它不应该类似于src="/public/images/mailjet/signup.jpg" 吗?
  • 我认为您将图像的名称误认为是文件目录。这些图像的标题为 mailjet signup.jpg 和 mailjet APIKey.jpg。不过感谢您的尝试。
  • 您的 Express 代码看起来不错,请尝试从文件名中删除空格(在文件名中包含空格通常是不好的做法)。将其更改为/images/mailjet signup.jpg 是否有效?
  • 我把空格去掉,改了目录路径也没有用。我班上的其他人说他们只是将图像上传到谷歌并使用链接。所以如果一切都失败了,我想我可以做到。

标签: html express handlebars.js


【解决方案1】:

要获取车把页面或 HTML 上的图像,我们需要在 index.js 文件或 app.js 文件中设置路径,这取决于您的起始页面是什么。

Index.js

app.use(express.static('views/images')); 

promotionapplication\views\images - 这是我的文件夹结构,我的 index.js 文件位于 promotionapplication,我将照片保存在 images 文件夹中。您可以保留文件夹的任何结构,但您应该在app.use 中相应地提及它。

所以现在我可以使用文件夹中的任何图片,通过在车把页面中使用以下代码 -

first.handlebars

 <img src="bckground.jpg" alt="piooop" /> 

请记住在进行更改后重新加载页面。

它工作得很好。如果有任何问题请评论,我会尽力给出答案。

【讨论】:

  • 现在回想起来,我发现我的代码中存在错误。我已经使用 express 注册了公用文件夹以提供静态文件,但我仍然在图像路径前加上 /public。所以我本质上是在 express.js 正在使用的公共目录中寻找一个名为 public 的目录。不过,您的回答在技术上是正确的。
【解决方案2】:

在 nodemailer 选项中使用附件。将 .hbs 文件中的图像 src 链接到附件描述中的唯一 cid,如下所示:

src="cid:unique@unique" 

 const mailOptions = {
from: 'xxxxx',
to: x,
subject: `xxxx`,
template : 'xxx',
attachments: [
  {
    filename: 'xx.png',
    path: __dirname +'/images/xx.png',
    cid: 'unique@unique'
  },]

【讨论】:

  • 这与我原来的问题有什么关系?
猜你喜欢
  • 2022-08-12
  • 2015-04-15
  • 2014-02-09
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
相关资源
最近更新 更多