What is Node.js? [closed]

Questions : What is Node.js? [closed]

I don’t fully get what Node.js is all about. Maybe it’s because I am mainly a web based business application developer. What is it and what is the use of it?

My understanding so far is that:

  1. The programming model is event driven, especially the way it handles I/O.
  2. It uses JavaScript and the parser is V8.
  3. It can be easily used to create concurrent server applications.

Are my understandings correct? If yes, then what are the benefits of evented I/O, is it just more for the concurrency stuff? Also, is the direction of Node.js to become a framework like, JavaScript based (V8 based) programming model?

Total Answers: 10 Answers 10


Popular Answers:

  1. I think the advantages are:

    1. Web development in a dynamic language (JavaScript) on a VM that is incredibly fast (V8). It is much faster than Ruby, Python, or Perl.

    2. Ability to handle thousands of concurrent connections with minimal overhead on a single process.

    3. JavaScript is perfect for event loops with first class function objects and closures. People already know how to use it this way having used it in the browser to respond to user initiated events.

    4. A lot of people already know JavaScript, even people who do not claim to be programmers. It is arguably the most popular programming language.

    5. Using JavaScript on a web server as well as the browser reduces the impedance mismatch between the two programming environments which can communicate data structures via JSON that work the same on both sides of the equation. Duplicate form validation code can be shared between server and client, etc.

  2. V8 is an implementation of JavaScript. It lets you run standalone JavaScript applications (among other things).

    Node.js is simply a library written for V8 which does evented I/O. This concept is a bit trickier to explain, and I’m sure someone will answer with a better explanation than I… The gist is that rather than doing some input or output and waiting for it to happen, you just don’t wait for it to finish. So for example, ask for the last edited time of a file:

    // Pseudo code stat( 'somefile' ) 

    That might take a couple of milliseconds, or it might take seconds. With evented I/O you simply fire off the request and instead of waiting around you attach a callback that gets run when the request finishes:

    // Pseudo code stat( 'somefile', function( result ) { // Use the result here } ); // ...more code here 

    This makes it a lot like JavaScript code in the browser (for example, with Ajax style functionality).

    For more information, you should check out the article Node.js is genuinely exciting which was my introduction to the library/platform… I found it quite good.

  3. The closures are a way to execute code in the context it was created in.

    What this means for concurency is that you can define variables, then initiate a nonblocking I/O function, and send it an anonymous function for its callback.

    When the task is complete, the callback function will execute in the context with the variables, this is the closure.

    The reason closures are so good for writing applications with nonblocking I/O is that it’s very easy to manage the context of functions executing asynchronously.

  4. Two good examples are regarding how you manage templates and use progressive enhancements with it. You just need a few lightweight pieces of JavaScript code to make it work perfectly.

    I strongly recommend that you watch and read these articles:

    Pick up any language and try to remember how you would manage your HTML file templates and what you had to do to update a single CSS class name in your DOM structure (for instance, a user clicked on a menu item and you want that marked as “selected” and update the content of the page).

    With Node.js it is as simple as doing it in client-side JavaScript code. Get your DOM node and apply your CSS class to that. Get your DOM node and innerHTML your content (you will need some additional JavaScript code to do this. Read the article to know more).

    Another good example, is that you can make your web page compatible both with JavaScript turned on or off with the same piece of code. Imagine you have a date selection made in JavaScript that would allow your users to pick up any date using a calendar. You can write (or use) the same piece of JavaScript code to make it work with your JavaScript turned ON or OFF.

  5. There is a very good fast food place analogy that best explains the event driven model of Node.js, see the full article, Node.js, Doctor’s Offices and Fast Food Restaurants – Understanding Event-driven Programming

    Here is a summary:

    If the fast food joint followed a traditional thread-based model, you’d order your food and wait in line until you received it. The person behind you wouldn’t be able to order until your order was done. In an event-driven model, you order your food and then get out of line to wait. Everyone else is then free to order.

    Node.js is event-driven, but most web servers are thread-based.York explains how Node.js works:

    • You use your web browser to make a request for “/about.html” on a Node.js web server.

    • The Node.js server accepts your request and calls a function to retrieve that file from disk.

    • While the Node.js server is waiting for the file to be retrieved, it services the next web request.

    • When the file is retrieved, there is a callback function that is inserted in the Node.js servers queue.

    • The Node.js server executes that function which in this case would render the “/about.html” page and send it back to your web browser.”

  6. Well, I understand that

    • Node’s goal is to provide an easy way to build scalable network programs.
    • Node is similar in design to and influenced by systems like Ruby’s Event Machine or Python’s Twisted.
    • Evented I/O for V8 javascript.

    For me that means that you were correct in all three assumptions. The library sure looks promising!

  7. Also, do not forget to mention that Google’s V8 is VERY fast. It actually converts the JavaScript code to machine code with the matched performance of compiled binary. So along with all the other great things, it’s INSANELY fast.

  8. Q: The programming model is event driven, especially the way it handles I/O.

    Correct. It uses call-backs, so any request to access the file system would cause a request to be sent to the file system and then Node.js would start processing its next request. It would only worry about the I/O request once it gets a response back from the file system, at which time it will run the callback code. However, it is possible to make synchronous I/O requests (that is, blocking requests). It is up to the developer to choose between asynchronous (callbacks) or synchronous (waiting).

    Q: It uses JavaScript and the parser is V8.

    Yes

    Q: It can be easily used to create concurrent server applications.

    Yes, although you’d need to hand-code quite a lot of JavaScript. It might be better to look at a framework, such as http://www.easynodejs.com/ – which comes with full online documentation and a sample application.

  9. Quote from “Eloquent Javascript” 3rd edition by Marijn Haverbeke:

    The difference in meaning between undefined and null is an accident of Javascript’s design, and it doesn’t matter most of the time. In cases where you actually have to concern yourself with these values, I recommend treating them as mostly interchangeable

    Honestly, at first, I am a bit skeptical about this advice. However, in my own interpretation, it is a lazy (vs eager) way to deal with their differences. Maybe, we don’t have to deal with the differences at all. If we have to, we can delay our concern (util we have to) and not hyperactively/defensively worry about it every step of the way as those values (null and undefined) flow through our code.

    PS: This is not a direct answer to your question. This is just a related opinion.

  10. The type of null is Object, while the type of undefined is undefined. Null means ‘no value’, while undefined means ‘not existing’.

    typeof undefined; //undefined typeof null; // Object undefined !== null; //true undefined == null; //true undefined === null; //false var var1; var1; //undefined  var var2 = null; var2; //null 
  11. Generally – don’t use null to avoid confusion.

    1. Standard library methods return undefined, not null
    let a = [10]; console.log(a[1]) //=> undefined console.log(a.find(value => value === 5)) //=> undefined 
    1. I see often in people’s code that some variable was undefined at first, then assigned to some value, then cleared by setting to null. That’s not consistent, better to set back to undefined.

    Still, null makes sense if framework uses it, or for json serialization.

  12. const data = { banners: null } const { banners = [] } = data; console.log(data) // null const data = { banners: undefined } const { banners = [] } = data; console.log(data) // [] 
  13. let a = null; console.log(a); // null let b; console.log(b); // undefined
  14. If a variable is not initialized then it is undefined. undefined is not a object. Example: var MyName; console.log(typeof MyName);

    Check the console log in development tool, it will be printed as undefined.

    null is a a object.If you want some variable to be null then null is used.null variable exists but value is not known.It should be assigned to a variable pro grammatically. null is not automatically initialized.

    Example : var MyName = null; console.log(typeof MyName); Check the csole log in development tool, it will be an object.

  15. Just to add my views –

    A variable that is declared only, and never used anywhere, is removed off by an optimizing compiler in case of compiled languages like C++[or a warning is flagged in the IDE]. It ultimately means that the variable is non-existent because it’s memory is never allocated.

    In case of javascript interpreter, [I guess] a variable is treated as existing only from the point onwards where it is given a value. Before that point, it’s type is “undefined”, and no memory is allocated for it. And, so its type is undefined.

    A null in javascript is a value that represents an address, but that address points to nothing yet[non-existent reference]. Nevertheless, its a value.

  16. Null is ALWAYS an unknown object that exists in memory, whereas undefined is not.

Tasg: javascript, node.js

Answer Link
jidam
  • Unable to run NoraUI mvn verify goal
  • Unable to run my app on emulator in VS Code
  • Unable to run multiple instances of libVLC(MobileVLCKit) in IOS via flutter framework
  • Unable to run make on griddb source on ubuntu 20.04 (building from source)
  • Unable to run latexindent macOS Monterey 12.0.1
  • Unable to run kotlinc-native command
  • Unable to run JUnit Test… Java.lang.ExceptionInInitializerError (Android Studio)
  • Unable to run java with -Xmx > 966m
  • Unable to run ionic cap run android from wsl2 inorder to start android emulator
  • Unable to run Intel HAXM installer: Cannot start process, the working directory does not exist
  • fs
  • Unable to run Google Analytics sample code
  • unable to run flutter run after upgarding to flutter 2.8.0 from 2.5.3
  • Unable to run Django with PostgreSQL in Docker
  • Unable to Run Container Using testcontainers
  • Unable to run ClojureScript Hello World program, Error building classpath. Error reading edn.
  • unable to run client command for apache karaf 4.3.3 through remote server
  • Unable to run c program 2nd time using eclipse
  • unable to run c++ in visual studio code on m1 chipset
  • Unable to run Android Instrumented Tests
  • Unable to run adb, check your Android SDK installation and ANDROID_SDK_ROOT environment variable: …AndroidSdkplatform-toolsadb.exe
  • Unable to run a singlespecific .spec.ts file through angular cli using ng test –include option
  • Unable to run a Mango query
  • Unable to return response back to view in laravel from package
  • Unable to return object reference in std::optional
  • Unable to return NULL in a function that expects an integer return type
  • Unable to return correct change in JavaScript Cash Register
  • Unable to retrieve version information from Elasticsearch nodes. Request timed out
  • Unable to retrieve values from Axios Response data
  • Unable to retrieve dotenv JWT secret Error: secretOrPrivateKey must have a value
  • Unable to resolve your shell environment
  • Unable to resolve token for FCM while implementing Push notification for Xamarin
  • Unable to resolve the request yii
  • Unable to resolve service for type Swashbuckle.AspNetCore.Swagger.ISwaggerProvider
  • Unable to resolve service for type Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger