从node.js Express服务器发送图像路径以响应客户端 [英] Send image path from node.js express server to react client
问题描述
我想构建存储图像的api并将其发送给客户端应用程序.
I want to build api that store images and send them to react client app.
图像位于Express应用程序的public/images文件夹中.
The images are located in the express app in the public/images folder.
我想使用内部图像路径将数据obj发送到客户端.
I want to send the data obj to the client with the image path inside.
Express.js:
Express.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cors = require('cors');
var index = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var signup = require('./routes/signup');
var clothing = require('./routes/clothing');
var app = express();
app.use(cors());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
app.use('/login', login);
app.use('/signup', signup);
app.use('/clothing', clothing);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
服务器:
const data = {
products: [
{
id: 1,
type: 'jeans',
price: 100,
brand: 'Levis',
size: ['s', 'm', 'l'],
color: 'Black',
image: '/images/levis.png'
},
{
id: 2,
type: 'shirt',
price: 150,
brand: 'Tommy Hilfiger',
size: ['s', 'm', 'l', 'xl'],
color: 'White',
image: '/images/tommy.png'
},
{
id: 3,
type: 'T-shirt',
price: 30,
brand: 'Levis',
size: ['s', 'm', 'l'],
color: 'Black',
image: '/images/polo.png'
}
]
};
router.get('/', function(req, res, next) {
res.send(data);
});
客户:
在React应用程序中,我想获取图像路径并进行渲染.
Here in the react app i want to fetch the image path and render it.
import React, { Component } from 'react';
import { withRouter, Link, Route } from 'react-router-dom';
import Shoes from './../Shoes/Shoes';
import axios from 'axios';
class clothing extends Component {
state = {
products: []
};
componentDidMount() {
axios({
method: 'get',
url: 'http://localhost:3001/clothing'
}).then(response => {
this.setState({
products: response.data.products
});
});
}
render() {
let productsList = this.state.products;
const products = Object.keys(productsList).map((product, id) => {
return (
<div key={id} className="product">
<img src={productsList[product].image} />
<h3>{productsList[product].brand}</h3>
<h3>{productsList[product].price}</h3>
</div>
);
});
return <div className="clothing-container">{products}</div>;
}
}
export default withRouter(clothing);
谢谢!
推荐答案
您在这里并没有提供太多代码,但是根据我的收集,您已经将图像保存在 public/images
目录中然后将图像路径发送回客户端,并希望客户端能够拉出图像.
You've not really supplied much code here but from what I gather you've got the images in a public/images
directory and then you're sending the image path back to the client and you want the client to be able to pull the image.
由此,我将假设您想要的是Express服务器静态功能.这将允许您提供 public/images
目录中的静态内容.
From that I'm going to assume what you want is the Express server static functionality. This will allow you to serve up the static content from the public/images
directory.
文档将向您展示如何设置投放静态文件,然后您的前端应该没有问题显示图像.
The documentation will show you how to setup serving static files and then your frontend should have no problems displaying the images.
这篇关于从node.js Express服务器发送图像路径以响应客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!