1

I'm currently learning React and have managed to get a good understanding of the front end. For ease of learning I started using the create-react-app boiler plate which uses Webpack & Babel.

I'm at the stage now where I want to have some interactivity with users uploading files and other account related activity like "liking" certain items in a list. I've gone with Firebase as my database for authentication and have a couple of questions regarding file storage and also processing user interactions with the web app:

Traditionally I have been using PHP to process user requests. IE a user would make an AJAX request with the target file being a php file that would take the posted data and then go on to make database updates or post uploaded files to a server. Should I use the same approach with my React app? I guess if I use this approach it would mean I would be hosting the visual part of my application on a node server and any php documents to process ajax requests on a php server?

An alternative that seems to be popular is using Node.js to handle user interactions with my database. Could I post similar Ajax requests to node files instead of php files in order to make database updates or file uploads? Is it generally safe to do so?

Apologies if these questions sound a bit rookie, it's a lot of new stuff to learn and i'm trying my best to get my head wrapped around it!

Thanks

red house 87
  • 1,045
  • 27
  • 66

2 Answers2

3

Node.js and PHP are the exactly same thing but using different languages.

You would also write a Node.js API that your frontend app would talk to.

You would also have a proxy in front, such as nginx or apache.

You would also have the same connection to the database.

I would strongly advise you to move on with Node.js and learn about Isomorphic Javascript that will help you share the same codebase between server and client and have the best performance possible.

kbariotis
  • 756
  • 1
  • 13
  • 24
  • Ok, so I could actually create endpoints written entirely in Node that interact with my DB for example? – red house 87 Mar 02 '17 at 17:59
  • 1
    Exactly! A request would leave client side and go through a proxy then to Node.js and then to the DB. Once it's finished, it will reply to the client. – kbariotis Mar 02 '17 at 18:00
  • 1
    [Here's a simple example](https://github.com/sequelize/express-example/blob/master/routes/index.js). If you hit the root of that app (`router.get('/', ...`), say https://api.domain.com/ that function would run and do a query to the database (`findAll`) – kbariotis Mar 02 '17 at 18:02
  • one more thing, is using node.js secure? Could someone view thing like bucket access keys that are stored in a node.js upload to server function for example? – red house 87 Mar 02 '17 at 21:14
  • 1
    Nop! Yet always remember to sanitize user's input before doing any processing with it. – kbariotis Mar 02 '17 at 21:24
2

When using a modern JS front end framework like React, it is helpful to interact with your database via RESTful API. Here is a decent explanation for what a RESt api is: https://stackoverflow.com/a/1081601/780286

Most modern web frameworks, say Ruby on Rails, Django or ExpressJS on NodeJS offer libraries for building RESTful apis. I suggest you start playing around in that space.

Community
  • 1
  • 1
Shawnzam
  • 140
  • 8
  • Ok, so before the data reaches the API what could I use as a middle tier? PHP or Node? (this tier would apply when sanitizing data before it reaches the API for example) – red house 87 Mar 02 '17 at 17:56
  • 1
    The API is the end point and you write _controller_ code, in whatever language you want(PHP, NodeJS...) to handle sanitizing, error checking, business logic. I suggest you read a bit about https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller – Shawnzam Mar 02 '17 at 18:02