0

I'm still exploring REST, node.js and generally web development. What I found out is that xmlhttprequest is mostly(if not always) used when using AJAX. As I learned AJAX is for asynchronous Javascript and XML. So my question is should I be using xmlhttprequest in my node.js project, just when I want to do asynchronous parts on my webpage? or does node.js HTTP also have opportunity to asynchronous javascript? How can I balance well the use of HTTP and xmlhttprequest(or AJAX) so that I don't get too messy in all my REST API stuff?

P.S. I kinda don't want to use AJAX, because of XML. I have heard that XML is much heavier in data than JSON and isn't worth using anymore. Is it true? What would you recommend me to do?

GreedyAi
  • 2,709
  • 4
  • 29
  • 55

1 Answers1

1

non async on node?

MRW reading node.js non async

you're trying to build an endpoint api so all the other cases of not using async should be thrown out the window. As soon as you have a single non async code in your node.js project it will freeze the entire process until it is complete. Remember Node.js runs a single Thread (theoretically) which means all the other concurrent users are gonna get frozen.. that's one way to make people really upset.

say for instance you need to read a file from your Node.js server on a get request from a client (let's say a browser) well you want to make it a callback/promise never do non-async with an API server there is just no reason not to (in your case).

example below

import * as express from "express";
import * as fs from 'fs';

let app = express();

app.get('/getFileInfo', function(req, res) {
    fs.readFile('filePath', 'UTF-8', function(err, data) {
        if (err) {
            console.log(err);
            res.json({error: err});
        } else {        
        res.json({data: data});
        }
    })
});

//users will freeze while the file is read until it is done reading
app.get('/nonasync', function(req, res) {
    let data = fs.readFileSync('path', 'utf-8');
    res.json({data:data});
});

the exact same idea applies to your web browser.. if you are going to not do something async in the browsers javascript the entire web application will be unresponsive because it also runs in the same manner, it has one main loop and unless they are in callbacks/promises/observable the website will freeze. Ajax is a much neater/nicer way to implement post/get/put/delete/get:id from a server then an XMLHttpRequest. now both of these have an option to send and receive JSON not only XML. Ajax is safer due to supporting different browser compatibility specs as XMLHttpRequest has some limitations in IE and Safari I believe.

NOTE: if you're not using a framework with node.js you should, it helps keep your endpoints neat and testable along with being able to pass the project on to others without them having to learn the way you implemented your req, res structure

some frameworks for node

  1. Express 4 (my preference, api doc is really really good and strong
    community)
  2. Restify (used by Netflix - really light)
  3. Hapi (never used but heard of)

some frameworks for web browsers you might like

  1. angular 2 (my preference as I'm from a MEAN stack)

  2. reactJS (created by big blue Facebook)

  3. knockoutJS (simple and easy)

all the browser frameworks have their own implementation of the RESTful api's, but more are leaning towards Observable objects.

Andrei
  • 1,196
  • 1
  • 11
  • 25
  • Andrei thanks for your answer. I thought AJAX was same as XMLHttpRequest. to be more correct I thought AJAX used XMLHttpRequest to do async stuff. I am using express and also want to try angular. I haven't used MEAN stack but really want to try whole stack. Your answer was really helpful, but got me still a bit confused. How do I use AJAX without using XmlHttpRequest? – GreedyAi May 27 '16 at 22:16
  • Ok I think I figured it out. I thought that there was only one AJAX library which was XmlHttpRequest. I figured out that there are lots of other libraries. Now everything makes sense :) – GreedyAi May 27 '16 at 22:52
  • 1
    here is a great SO explanation, sorry for confusing you! what I was trying to get across is that the libraries wrap around the XML request and allow more usability and give more API to use rather then the base XMLHttpRequest, think of it as extending XMLHttRequests -> better answered [here](http://stackoverflow.com/questions/4657287/what-is-the-difference-between-xmlhttprequest-jquery-ajax-jquery-post-jquery) but to address your concern of xml vs json, you just need to specify it in the request you make. essentially XMLHttpRequest is the browser API to use the HTTP protocol. – Andrei May 27 '16 at 23:33