如何使用节点 js 的 ejs 模板引擎显示图像标签?

How to display an image tag with the ejs template engine for node js?

一些背景信息:我正在使用 node.js 和 express 框架以及 mongo 和 passport.js

所以基本上我能够使用 FB 登录名登录到我的应用程序并检索一些信息,如用户名、令牌、ID、显示名称和照片。然后将此数据保存到我的数据库中 (mongo)。

除了图像,我可以使用 ejs 语法读取视图中的所有数据。

在我看来,我有以下图像代码:

<%= user.facebook.profilePic %>

这个returns下面的输出:

https://graph.facebook.com/Grace Yip/picture?type=large

但是当我在浏览器中浏览这个 url 时,我收到以下错误消息

error: {
      message: "(#803) Some of the aliases you requested do not exist: Grace      Yip",
      type: "OAuthException",
      code: 803
}

我尝试将我的数据输出放在图像标签中,如下所示:

<%= img_tag( user.facebook.profilePic ) %>

但随后我收到一条错误消息:错误:无法在视图目录“/Users/Grace/Desktop/QA/views”中查找视图 "error"。

当我删除 img_tag 代码时,视图再次工作。

不知道如何显示图片...我很茫然。

提前致谢!

你好,

恩典

您需要使用 JavaScript 内置函数 encodeURI 对 URI 进行编码,因为 URI 中包含一个 space,这是无效的:

<%= img_tag(encodeURI(user.facebook.profilePic)) %>

我在用 user.facebook.profilepic is bson string path image http from graph facebook api with passport.js 你可以试试申请bson str path

<img src="<%= user.facebook.profilepic %>" alt=""> 在 .ejs 文件中