简体   繁体   English

在React中使用Node Module

[英]Using Node Module With React

I think, because I'm so new to Node, I'm not quite understanding how to use an NPM package with my React project running on Node. 我想,因为我对Node还是很陌生,所以我不太了解如何在Node上运行我的React项目时使用NPM包。

Just a quick explanation: 简要说明一下:

I have a React component that uploads a zip file through Node server scripts. 我有一个React组件,它通过Node服务器脚本上传一个zip文件。 This part is working great, but now the next step I'm working on is getting node to unzip it after upload into a temp folder.. 这部分工作很好,但是现在我要进行的下一步是让节点将其上传到临时文件夹后解压缩。

I'm using "axios" to post, and "multer" to save to file system and "adm-zip" for the unzip portion. 我正在使用“ axios”发布内容,并使用“ multer”保存到文件系统中,并使用“ adm-zip”保存解压缩部分。

Here's a quick look at my on submit post function within my React Component: 快速浏览一下我的React组件中的on Submit post函数:

onSubmit = (e) => {
    e.preventDefault();
    const { description, selectedFile } = this.state;
    let formData = new FormData();

    formData.append('description', description);
    formData.append('selectedFile', selectedFile);

    console.log('form data ',formData)

    let that = this;

    axios.post('/', formData)
      .then((result, req) => {
        this.setState({imagePath: result.data.path})
        this.setState({fileName: result.data.filename})
        this.setState({fileUploadStatus:"Upload Successful.."})
        Unzip.extract(result.data.filename);
      })
      .catch(function (error) {
        console.log(error);
        //alert('File upload failed. Please ensure you are uploading a .zip file only')
        that.setState({fileUploadStatus:"Upload failed.. Please ensure you are uploading a .zip file only"})
      })
  }

This is what I've put on the top of the React Component : 这就是我放在React组件顶部的内容:

import React, { Component } from 'react';
import axios from 'axios';
import Unzip from './unzip';

This is my "unzip.js" file: 这是我的“ unzip.js”文件:

var AdmZip = require('adm-zip');

// reading archives

module.exports = (function() {

  var extract = function extract(fileName, cb){ 

    zip = new AdmZip("../uploads/"+fileName);
    var zipEntries = zip.getEntries(); // an array of ZipEntry records

    zipEntries.forEach(function(zipEntry) {
      console.log(zipEntry.toString()); // outputs zip entries information
      if (zipEntry.entryName == "my_file.txt") {
           console.log(zipEntry.getData().toString('utf8')); 
      }
    });
    // extracts everything
zip.extractAllTo(/*target path*/"/home/me/zipcontent/", /*overwrite*/true);

  };

});

So the part I'm struggling with is understanding how to write a node script javascript file) with functions/properties (getter, setter, etc) that can be accessed from my React component.. 因此,我正在努力的部分是了解如何编写具有可从我的React组件访问的函数/属性(getter,setter等)的节点脚本javascript文件。

I tried something like this: 我尝试过这样的事情:

var AdmZip = require('adm-zip');

// reading archives

    var extract = function(thePath) { 

        zip = new AdmZip("../uploads/"+thePath);
        var zipEntries = zip.getEntries(); // an array of ZipEntry records

        zipEntries.forEach(function(zipEntry) {
          console.log(zipEntry.toString()); // outputs zip entries information
          if (zipEntry.entryName == "my_file.txt") {
               console.log(zipEntry.getData().toString('utf8')); 
          }
        });
zip.extractAllTo(/*target path*/"/home/me/zipcontent/", /*overwrite*/true);
        // extracts everything

      };

But I got errors saying the function did not exist.. so I realized that I might have to set it up as a Node Module with an export function, but I couldn't figure out how to get that to work either.. 但是我收到错误消息,说该功能不存在..因此,我意识到可能必须将其设置为带有导出功能的节点模块,但是我也无法弄清楚该如何工作。

Can someone explain to me, the proper way to approach this type of external script (script that runs outside of React and returns or executes some kind of action)? 有人可以向我解释处理此类外部脚本(在React外部运行并返回或执行某种动作的脚本)的正确方法吗?

As I mentioned in some of my other recent questions, I'm a beginner with Node so please go easy on me :) 正如我在最近其他一些问题中提到的那样,我是Node的初学者,所以请放轻松我:)

Thanks! 谢谢!

React is a frontend package. React是一个前端包。 You can use tools like unzip on the backend side. 您可以使用后端解压缩之类的工具。 for example with ExpressJS. 例如ExpressJS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM