【发布时间】: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应该有空格吗?例如<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