Accessible file upload in React (no drag/drop, no jQuery) - reactjs

I am trying to find an accessible file upload method for React that doesn't rely on jQuery (I am not using it) but does rely on Fetch (async).
Everything I've found thus far seems to be drag and drop type components or uses jQuery's $.ajax methods. I feel like there's got to be a way to send a file with other form fields.
The front end is React with vanilla JavaScript. I've got a custom API that sends data to my back end asynchronously with Fetch. My back end uses Multer to gather form data. My form sends with multipart/form-data.
I've tried a few things, including adjusting the headers when sending from React to my back end, but either only the body comes through (no files) or nothing at all comes through.
I could get this to work by directly sending to my Express server, but I don't want to expose my API routes in the HTML, and I'm hoping to avoid the page refresh which would make this form stand out from everything else in my app. I have decided against using jQuery because I am able to do pretty much everything without it and I don't want to add the weight of the library just for the one method.
Accessibility is key (drag and drop components generally aren't accessible for obvious reasons) so I'm hoping to use a standard input file element here.
Thanks to anyone and all in advance.

Related

How to embed or get metdata from a link?

I am trying to make React Component where I like to display cards like which you see on Facebook or Linked In when you post some link.
You start with fetch() and parse the information from the return HTML, mainly in elements inside the <head>.
However, this is only going to work if the target site has super open CORS headers. Most sites don't, and for those cases you will need a server to do the fetching and perhaps parsing.

Passing around data

My task is to create a SPA with an iTunes API.
I got my basic functionality done but it's not SPA yet, i wanted to implement react-router to handle the navigation.
However my components now need an extra parent. And i don't quite get it how can I pass the previously working data.
I don't want to inject huge block of code so i'd rather give you a Github link which is:
My SPA
But if you need the snippets let me know and i'll cover it.

history.js and angular or ember

URLs shall be without hashbang
It shall fall back for older Browsers which don't support the History API
Using Angular or Ember
Question: Is there a need to use the history.js?
If yes, read on.
On teamwork.com or soundcloud or other modern websites there is a mechanism to have links loading just a part of the website and at the same time, the URL changes (there is no hashbang). I want to implement this as well and also want to support older browsers and read that history.js can do this. Here is a related question to this where I found that this uses the History Api.
At the same time I want to uses Angular or Ember. I know they have their own routing and there is for example a tutorial to remove the # here.
I read that the different browsers handle the history api differently and that the history.js is a way how to tackle this issue.
So is there a way how to combine those front-end frameworks with that api? I'd start somewhere in the routing but get stuck thinking about what to do...
A new implementation should have cross-browser compatibility including handling older browsers. On one hand I want to have a front end framework to separate front from the backend, not sure about which one to use, yet. On the other hand I like how i.e. teamwork.com handle links that they only load a part of a page (including an animation) when you click on it. At the same time the URL changes and if you type a URL directly the page is loaded correctly. It seems that they do it with jQuery.. not quite sure.
Anyone knows how to use both, routing and the history.js?
I would just use AngularJS with UI Router. It combines routing with history.js like features. It has a HTML5 mode which you can enable which will allow pages to update via AJAX without the use of a hash #.
It should automatically cope with older browsers and change the URL format accordingly.

How to create a dynamic front end based on Node JS, MongoDB, Sails JS

Basically I'm writing an app and am using Sails, MongoDB and Node JS for the back end. I'll use Sail's API features and was wondering what would be the best way to make the app realtime.
For instance I could use AJAX to call the API and manipulate the DOM using jQuery and update the DB through $.post then let the model update the db in the backend, however I'm finding this approach quite cumbersome. Not to mention I can see the code could become quite difficult to maintain after a while.
I've been doing some research and - if I understood correctly - it seems I could use either Backbone, Angular or Knockout to manipulate the data/DOM on the front end, however I'm not sure what would be the best approach in my case nor whether any of these would indeed suit my needs:
Being able to get the data dynamically
Update the data and the DOM dynamically as the user interact with the page
Post the updated data dynamically with none or as minimal data transformation on the back end as possible
All the above asynchronously
As I don't want this to become a heated debate on which library is best, so I would like to know only whether any of the aforementioned libraries can do what I need and which is the leanest/simplest/has the lighter learning curve.
I did similar research a while ago and when found AngularJS, just stopped looking any further.
Right to your questions:
Being able to get the data dynamically
It is pure pleasure to do it in Angular. For the very basic functionality you have got $http service which allows you to send http request and register a callback when the data arrives.
For more complicated things there are modules ngResource and Restangular (external).
Update the data and the DOM dynamically as the user interact with the page
For manipulating DOM, Angular introduced concept of directive. It is basically future of the web (Shodow DOM and Web Components) right now. At this time point, there is nothing more elegant out there.
Post the updated data dynamically with none or as minimal data transformation on the back end as possible
Yes. JSON.
All the above asynchronously.
Yes, of course.
SailsJS provides interchangeability of HTTP or socket.io connections. In your case I think sockets would be a better fit than AJAX.

AngularJS getting data from backend

I would like to know what is the proper way to get data from backend when I want to use angularJs (or similar) in my web app?
The only way I see is to render html (static html with js scripts - e.g. angularjs) with no data from backend and then download data via ajax requests from my backend API. But I think this solution is not good because of many HTTP requests:
For example I have blog website, I want to show a post, comments, and the related posts on the sidebar. So probably I need to make at least 3 HTTP requests to get the data unless I will prepare API to get all I need in one request.
I can also imagine websites that could have much more HTTP requests. Is it a proper way to do this? Doesn't it overload a server? Or my way of thinking is so wrong?
It is either websockets or HTTP requests. Preparing API to get all in one request is one option. Another two options are XMLHttpRequest/iframe streaming which is a method of a technique known as Comet.
I would go with websockets since it is supposed to solve the problem that was previously solved with weird applications like iframe streaming. There are libraries that properly handles fallbacks if the browser does not support websockets:
web-socket-js ( this needs a websocket server )
Socket.IO ( this has a node.js module and also implements a kind of unnecessary protocol on top of websocket protocol )
If you choose the old methods there will be many problems waiting for you on the road like XmlHttpRequest.responseText while loading (readyState==3) in Chrome
I think you have to distinguish two cases:
You render the page for the first time.
You update parts of your page when something changes
Of course in the second case it makes sense to fetch only parts of the page via individual HTTP requests. However, in the first case you can simply serialize your complete model as one JSON object and embed it in the page like this:
<script type="text/javascript">
var myCompleteModel = { /* Here goes your model */ };
<script>
The controllers of the components on your page can then access this global variable to extract the parts being relevant for them. You can also wrap access to the initial model in a service to avoid accessing a global variable in all your controllers.

Resources