staltz / react-native-node

Run a separate Node.js process behind a React Native app
MIT License
1.12k stars 33 forks source link

Bunch'o questions #19

Closed xemasiv closed 6 years ago

xemasiv commented 6 years ago

Hi I'm really amazed by the project.

Anyways:

I know this isn't stackoverflow but I'm really clueless about the posaibility, or if so, how.

Thank you.

staltz commented 6 years ago

Hi!

Those two things are theoretically possible if you modify the Java source (in other words, change this library a bit), but currently it only spawns one process. Maybe node's child_process will work, I'm not sure. Want to try it out? :)

xemasiv commented 6 years ago

Just tested.

And lol, yes we can.

Did the magic in background\index.js:

const express = require("express");

const app = express();

let x = 0;
app.get("/", (req, res) => {
  x = x + 1;
  switch (x) {
    case 1:
      res.json({
        msg: Object.keys(global).toString()
      });
      break;
    case 2:
      let ls = require('child_process').spawn('ls');
      let datadata = [];
      ls.stdout.on('data', (data) => {
        datadata.push(data);
      });
      ls.stderr.on('data', (data) => {
        datadata.push(data);
      });
      setTimeout(() => {
        res.json({
          msg: datadata.toString()
        });
      }, 500);
       break;
    case 3:
       res.json({
         msg: require('fs').readdirSync('/').toString()
       });
      break;
    default:
      x = 0;
      break;
  }
});
app.listen(5000);

At first I thought they were just random strings / gibberish, yet turns out it's root already.

Initially got error in using dir as test command too, turns out ls is the right one.

Refs: