代码之家  ›  专栏  ›  技术社区  ›  jrocc

express服务静态图像以响应本机应用程序

  •  0
  • jrocc  · 技术社区  · 6 年前

    我正在尝试将一个静态图像从node js express服务到我的react本地应用程序。

    我正试图使用express.static,但我发现了错误

    失败的属性类型:提供给映像的属性源无效。

    如何为React本机应用程序提供静态图像?

    文件夹结构:

    --public
    -image.png
    -app.js
    

    App.JS

    const app = express();
    
    app.use(express.static('/public'));
    
    app.get('/', (req, res) => res.status(200).send({
      message: 'API Called',
    }));
    
    module.exports = app;
    

    HeopePa.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>
        );
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   jrocc    6 年前

    我想我得用

    app.use('/image', express.static(__dirname + '/public'));
    

    在app.js中。

    然后要显示图像,我必须用公共文件夹中的图像名调用图像路由。

    source={{uri: 'http://192.168.1.10:8000/image/image.png'}}