node.js - 将静态图像快速提供给 React-native 应用程序
问题描述
我正在尝试将来自 node js express 的静态图像提供给我的 react 本机应用程序。
我正在尝试使用 express.static 但出现错误
失败的道具类型:提供给图像的道具来源无效。
如何将静态图像提供给 react-native 应用程序?
文件夹结构:
--public
-image.png
-app.js
应用程序.js
const app = express();
app.use(express.static('/public'));
app.get('/', (req, res) => res.status(200).send({
message: 'API Called',
}));
module.exports = app;
主页.js
import React, {Component} from 'react';
import {AppRegistry, Text, View, TextInput, Button, StyleSheet, Image, } from 'react-native';
export default class HomePage extends Component{
constructor(){
super();
render(){
return(
<View style={styles.ViewStyle}>
<Image
style={styles.Image}
//{uri: this.getImage(item.teampicture1)}
source={{uri: 'http://192.168.1.10:8000/image.png'}}
/>
</View>
);
}
}
解决方案
我发现我必须使用
app.use('/image', express.static(__dirname + '/public'));
在 app.js 中。
然后为了显示图像,我必须使用公共文件夹中的图像名称调用图像路由。
source={{uri: 'http://192.168.1.10:8000/image/image.png'}}
推荐阅读
- javascript - 在下一个 js 中使用浏览器历史状态
- node.js - Mongoose:返回一个自定义对象作为对 POST 请求的响应
- python - 在文件中查找特定的 Python
- python - 给定序列的子序列/切片的策略(子列表/子元组/子字符串/等)
- lambda - Isabelle 中恼人的替换行为来自 lambda 函数应用程序
- sql - 如何找到纬度值并将其保存在postgresql的另一个表中
- rundeck - 作业条件报告以前的作业“从不运行”
- unity3d - Unity2D 没有正确检查条件
- mongodb - Docker中的SpringBoot未连接到Mongo并引发异常
- amazon-web-services - Amazon Elastic Container Service 中的无法拉取容器错误