express.res.write()对于图像标签不起作用

问题描述 投票:0回答:2

我是快递的初学者,我写了代码

res.write("<img src='"+"http://openweathermap.org/img/wn/[email protected]" + "'>");

我希望它向我显示此url中的图像,但它向我显示了在引号下写为屏幕文本的完整代码。我该如何解决这个问题

node.js express
2个回答
0
投票

您可以使用res.send()发送纯HTML:

res.set('Content-Type', 'text/html');
res.send(new Buffer("<img src='" + "http://openweathermap.org/img/wn/[email protected]" + "'>"));

0
投票

res.write()用于发送响应内容(可能作为网页);因此,在编写所需内容之后,应调用res.end()或直接使用res.send()。

要显示图像文件,您应该提供静态文件:

app.use(express.static('the/path/to/your/image/file'));

app是您在开始时创建的express()的对象。

© www.soinside.com 2019 - 2024. All rights reserved.