Vato Vato - 2 years ago 357
Ajax Question

Should I use HTTP or xmlhttprequest on node.js? When?

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?

Answer Source

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) {
            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');

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
  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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download