Running JavaScript: browser or server [3 of 51]

Play Running JavaScript: browser or server [3 of 51]

The Discussion

  • User profile image
    girmamoges
    Distinctly clear and I like it.
  • User profile image
    pramgoat
    In order for these instructions to work, properly...first, you have to have...

    a> Window 10 Professional operating system

    (NOTE: Other Windows platforms may work, too: Win XP/7/VISA/8/-etc.; I'm just stating the particular platform I'm using, at the moment.)

    b> NODEJS already downloaded/installed

    (go to: nodejs.org, for the FREE download)

    -----

    1> On my Windows Desktop, I created an empty folder called: node-test-files; then, left double clicked on this folder in order to open it up...

    (this folder will be used to store all of my program files; and, I intend to create 2 separate program files with extensions: [.bat]/[.js])

    HOW TO CREATE A FOLDER
    Right click an 'empty space' inside of the Windows Desktop; a context menu drop down list of menu options appears, select: New -> Folder

    HOW TO RENAME A FILE/FOLDER
    You can left click on the folder/then, either right click and choose option: Rename/
    or, left click to select the folder/then, press function key [F2] to Rename

    -----

    2> I opened Windows Notepad...which allows me to create plain text files: [.txt]/-(as well as, many other types of program file: [.js]/-etc.)-. Inside of the newly opened Notepad Window I wrote the follow 2 lines of code:

    @ECHO OFF
    CMD

    ...saved this file as being called: run-dos.bat (a Windows batch file program)

    HOW TO OPEN UP A FOLDER YOU'VE JUST GONE AND CREATED
    First, left double click to open up the folder: node-test-files

    HOW TO OPEN WINDOWS NOTEPAD APPLICATION IN ORDER TO CREATE A 'NEW TEXT DOCUMENT' FILE
    Next, right click an 'empty space' inside of the node-test-files folder
    from the context menu a drop down list of options appears choose: New > Text Document

    -----

    3> I opened up another Windows Notepad file...and, typed in the code as was suggested inside of the video...

    const fileSystem = require("fs");
    fileSystem.writeFileSync("hw01.txt","Hello, world!");
    console.log("The file: hw01.txt, has been created.");

    ...saved this file as being called: write-file-01.js

    -----

    4> I left clicked on the file: run-dos.bat
    this opened up a new MS DOS Prompt style window -(with a black background screen)- ready for typing further instructions into...
    To make sure my files are all inside there...I typed...

    C:\user\desktop\node-test-files>DIR [then, press the ENTER/or, RETURN key]

    ...this should give a full DIRectory listing of all files available in the current folder DIRectory.

    5> Next, I typed the instructions...

    C:\user\desktop\node-test-files>NODE write-file-01.js

    ...an MS DOS console message appeared stating...

    The file: hw01.txt, has been created

    6> You can check this by either typing in after the MS DOS prompt...

    C:\user\desktop\node-test-files>DIR [then, press the ENTER/or, RETURN key]

    ...and, you should see a 'new' file appear inside of the folder DIRectory listing called: hw01.txt

    ..still using the Windows CLI/Command Line Interface...(DOS Black screen window)...type in the instruction...

    C:\user\desktop\node-test-files>TYPE hw01.txt [then, press the ENTER/or, RETURN key]
    ...and this should display inside of the black MS DOS Prompt window all of the text the file: hw01.txt contains

    Or, alternatively, using the 'default' Windows GUI/Graphical User Interface...
    simply, look inside of the folder you created before called: node-test-files;
    and, inside there you will see the file called: hw01.txt...left double click the file in order to open it up/and, view it's output contents.

    -----

    NOTE: Thanks very much to Microsoft/and, also, the creator of this video; which I found to be very helpful, indeed. By following along the instructions -(pausing the video whenever necessary)- I found it was surprisingly easy to use NodeJS to create Windows program files.
  • User profile image
    pramgoat
    I later on when and 'modified' the output file to show multiple output lines, instead...

    (NOTE: The backwards slash + n: '\n', is a new line character.)

    const fileSystem = require("fs");
    var multipleLines = "line 1\n"
    + "line 2\n"
    + "line 3";
    fileSystem.writeFileSync("hw02.txt",multipleLines);
    console.log("The file: hw02.txt has been created.");
  • User profile image
    pramgoat
    This time I decided to adapt the same code to output one of my favourite poems by Emily Bronte.

    -(NOTE: I used one '\n' to produce a single line break; and, two '\n\n' to produce double line breaks.)-

    const fileSystem = require("fs");
    var multipleLines = "Fall leaves, fall,\n"
    + "Die flowers away,\n\n"
    + "Lengthen night,\n"
    + "And, shorten day.\n\n"
    + "Every leaf speaks bliss to me,\n"
    + "Fluttering from the Autumn tree,\n\n"
    + "I shall smile when wreathes of snow,\n"
    + "Blossom where the rose should grow;\n\n"
    + "I shall sing when nights decay,\n"
    + "Ushers in a drearier day.\n\n"
    + "-Poem: Emily Bronte";

    fileSystem.writeFileSync("hw03.txt",multipleLines);
    console.log("The file: hw03.txt has been created.");
  • User profile image
    pramgoat
    Finally, added the code into my own GitHub a/c.; so that I can further get to use and play around with it, in future.

    https://github.com/pramnora/NodeJS/tree/master/language/filing/write

    -----

    NOTE: Is there any such thing as producing a 'final' commit...whenever it comes to learning the art of programming? The truth is, code always has the potential for creating 'endless' possible variations...the which process is only 'limited' by one's own imagination.
  • User profile image
    pramgoat
    I just wanted to do a few other additional tests...

    - in this particular case, I wanted to see if NOdeJS would allow me to create/write a web page file.

    The answer is, yes.

    -(NOTE: I had to include: '\t', for tab space; and, '\n' for new lines. Frankly, HTML does not require these...; however, when you click on the 'webpage01.html' file to load it up for viewing inside of your web browser...I'm using Google Chrome web browser; then, when you try viewing the underlying 'source code' there is no automatic word wrapping; thus, I decided to add these things, manually, instead.)-

    const fileSystem = require("fs");
    var webPageContent = "\n"
    + "\n"
    + "\t\n"
    + "\t\t\n"
    + "\t\t\n"
    + "\t\n"
    + "\t\n"
    + "\t\t

    Hello, world!

    \n"
    + "\t\n"
    + "";
    fileSystem.writeFileSync("webpage01.html",webPageContent);
    console.log("The file: webpage01.html, has been created.");
  • User profile image
    pramgoat
    Unfortunately, the HTML codes which I posted above...did NOT display inside of the comments posting...(maybe, I should have surrounded it with PRE-/PRE tags)?! I guess, that must mean...because this is, in fact, a web site page into which I'm posting HTML code...maybe, HTML code inside of comments has been 'disabled'...so, instead, all that showed up was: '\t' & '\n'. LOL Well, nevermind, the HTML 'source code' can still be seen/read in all of it's entirety here:

    https://github.com/pramnora/NodeJS/blob/master/language/filing/write/write01d.js
  • User profile image
    pramgoat
    Ok, just one more test...; before I leave.

    Because, any one single programming language...that is able to output 'plain text' files...;
    which may be any filename/or, extension you -the program writer- do please...;
    naturally, it therefore follows that..it's possible to use any programming language
    to 'write/output' any other programming language code.

    To put this a bit more clearly...NodeJS...can be used to write/output:
    - BASIC code (.bas)
    - C code (.c)
    - CPP code (.cpp)
    - Java code (.java)
    - HTML code (.html)
    ...or, indeed, any other programming language code one could ever even possibly 'think' of...?

    Previously, I showed how it's possible to use NodeJS code to 'write/output' HTML web page files.

    In this example, I will show how it's possible to use NodeJS code to 'write/output' Python files...

    const fileSystem = require("fs");
    var pythonCodeContent = "print('Hello, world!')"
    fileSystem.writeFileSync("hw01.py",pythonCodeContent);
    console.log("The file: hw01.py, has been created.");

    NOTE: The above program will output a file called: hw01.py; but, in order to 'test' if the above Python code actually works...by running the Python program this program outputs...; then, you will need of have the Python programming language downloaded/and, installed on your operating system, first.

    Python, can be downloaded for FREE from: python.org

    Alternatively, if you were to 'copy and paste' the code that has been output: print('Hello, world!') into an online Python editor...you can test it out that it works in this way, too(without needing to download anything). They have a FREE online Python playground in which you can both type in/and, run Python code...at: python.org. Sincerely, I wish you very good luck with it! ;)

    -----

    Btw, the GitHub link to this particular file 'source code' is here...

    https://github.com/pramnora/NodeJS/blob/master/language/filing/write/write01e.js
  • User profile image
    pramgoat
    NOTE: When it comes to writing code...using Windows Notepad is NOT the best programming language editor...; there are, in fact, many code editors which are far better.

    Other far more 'advance' code editors offers you many extra features such as...

    - Add on's...a choice of multiple different add on's you can get to select from
    - Automatic syntax highlighting/(using multiple different programming languages syntax)
    - Automatic code completion (helps suggest code as you type)
    - MDI-Multiple Document Interface, inside of the one same editor...multiple tab windows can all be opened together, at once
    - Advance editing (multiple cursors)
    - etc.

    ...none of the above features are included with Windows Notepad; so, it is a very simple basic plain text editor, indeed. (I only ever use it to write short code snippets, alone...when it comes to writing much lengthier stuff...I prefer using something a lot more advanced...as it improves coding development speed.)

    Other code editors you can download...(most free/or, at least, on a trial basis)- and, use are...
    - Notepad++
    - Bracketts
    - Atom
    - Sublime Text
    - Visual Studio Code
    - (...and, there are many MANY more...)
    - etc.

    In addition to which, if you are going to be a 'flexible' programmer; then, it's good to know how to use as many different program editors/as well as, operating system platforms as you can; as not every computer operating system may have to exact editor you prefer.
  • User profile image
    popehiflo
    Thanks!

Add Your 2 Cents