简体   繁体   English

如何使用node js中的express通过server.js文件将照片渲染到网页

[英]How to render a photo via server.js file to webpage using express in node js

I am currently working on a project where I have to do a lookup page in node js. 我目前正在一个项目中,我必须在node js中执行查找页面。 The lookup needs to lookup an id (entered in my rendered index.ejs file by the user) and display the information retrieved from a database. 查找需要查找ID(用户在我呈现的index.ejs文件中输入),并显示从数据库检索到的信息。 Information recieved includes the id, name, email, and a picture. 收到的信息包括ID,名称,电子邮件和图片。 So far I have figured out how to render text into the ejs file however I cannot find any information on how to live render an image from the server.js file. 到目前为止,我已经找到了如何将文本渲染到ejs文件中的方法,但是我找不到关于如何从server.js文件实时渲染图像的任何信息。 I've looked for ways to render an "" tag through the text so I can load it that way and I have looked for ways to render the image itself but have only been able to find tutorials on how to make a html file with a hard coded image inside, and render the whole file, which would not allow me to do the lookup. 我一直在寻找在文本中呈现“”标记的方法,以便可以以这种方式加载它,并且我一直在寻找呈现图像本身的方法,但仅能找到有关如何使用html文件制作html文件的教程。里面的硬编码图像,并渲染整个文件,这使我无法进行查找。 Can anyone suggest a way to do this and/or link me to somewhere where I can get the information on how to do this? 谁能建议一种方法和/或将我链接到可以获取有关此方法信息的地方?

.

Key details (as I do not know what you need to know): 关键详细信息(因为我不知道您需要知道什么):

File type: ejs 文件类型:ejs

File render type: res.render() 文件渲染类型:res.render()

Main file: server.js 主文件:server.js

.

Im sorry if this isnt much to go on as I am new to node js. 对不起,如果这不是很多事情,因为我是Node js的新手。

Thanks 谢谢

If you have full image in the database, then 如果数据库中有完整图像,则

There is pic attribute to the res.render in express.js An example of how you can render image is below. express.js中的res.render有pic属性。下面是一个如何渲染图像的示例。

var pic = "./public/images/"+collection_id+"/"+photo_id+".png"; //(You can get the image from database and assign here)
var comments = "bla bla bla";

res.render('index', { title: 'Viewer' , pic: pic , comments: comments } );

In your ejs: 在您的ejs中:

<img src='<%= pic %>' />

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

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