nodejs服务器的图片如何在网页上能够看到

版权声明:可供读者学习参考使用 https://blog.csdn.net/weixin_40169642/article/details/83107234

这里给出的是基于express框架的,图片放在项目根目录下的public/images,为了省事,直接写在app.js里面,不要写在路由那边,不然路径一改就要去手动改动。

代码:

//图片加载,存储在public/images下的所有图片
app.get('/public/images/*', function (req, res) {
    res.sendFile( __dirname + "/" + req.url );
    console.log("Request for " + req.url + " received.");
})

效果图:
在这里插入图片描述

每天进步一点点,开心也多一点点

猜你喜欢

转载自blog.csdn.net/weixin_40169642/article/details/83107234