Home > Blockchain >  How to make reactnative interact with node.js on vs code?
How to make reactnative interact with node.js on vs code?

Time:12-13

I want to work with reactnative and node.js both on visual studio code in the same project. But I'm having trouble connecting between them.

A lot of resources on the internet weren't practical or useful.

CodePudding user response:

It should be fairly straight forward but here's a simple guide on how to with both easily:

  1. Create a folder for the entire project

  2. Create two subfolders, one for your react native project (I usually name it app) and one for your server (node.js).

  3. Make sure to install nodemon in your server folder, and add this part in your package.json

    "scripts": {
    "start": "node app.js",
    "devstart": "nodemon app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    

    Notice that I added the part called "devstart". Then you simply run "npm run devstart". Now your Node.js project is running and is reloading whenever you make changes.

You can now make API calls to your node.js backend, as long as you use the correct port number etc. I usually use Axios package for both ReactJS and React Native, I strongly recommend it!

Hope this helps!

CodePudding user response:

First, you'll need to understand the difference between code running on the server (nodejs) and code running on the client (reactnative).

The Nodejs code you're writing is propably for a webserver. This webserver is running on a server (not on the device of the reactnative client). To access this webserver, you can make http calls (e.g. webserver.com/api/hello could return the JSON { 'value': 'hello' }.

Your reactnative application runs on a specific device. If your server is running it's possible to make http calls to the server.

  • Related