• Node.js 101

    1. Read-Eval-Print-Loop (REPL)

    • Node provides an interactive language shell, known as the Read-Eval-Print-Loop, or REPL.
    • REPL reads inputs from a user, evaluates the inputs as JavaScript codes, prints out the results, and then waits for more inputs.
    • REPL is useful for debugging and for experimenting with small snippets of JavaScript codes.
    1) Starting the REPL and inputting some codes
    • The following shows how to start the REPL and input Javascript codes.
    • A variable, named foo, is created with the string value “Hello Node.js!”.
    • REPL prints “undefined” because the variable declaration statement returns no value.
    • Next, the statement foo; causes the value of foo to be inspected.
    • As expected, REPL returns the string “Hello Node.js!”.
    • Finally, the value of foo is printed to the terminal using the console.log() function.
    • After foo is printed, REPL displays “undefined” again, because console.log() returns no value.

    2) Multiline lines of expression in REPL

    3) REFL functions
    • Control + C: terminate any command and return to a blank prompt
    • Pressing Control+C twice from a blank line: REPL comes to end
    • Control + D: you can quit the REPL at any time.
    • Tab key: see a list of possible autocompletion optionss to the current command.
      • If there is only one possible option, Node automatically inserts it.
      • The list includes keywords, functions, and variables.

    • _ (underscore): it contains the result of the last expression.

    4) REFL commands
    • .help: displays all of the available REPL commands
    • .exit: terminates the REPL
      • equivalent to pressing Control+D
    • .break: exits out of a multiline expression.
      • it is useful if you make a mistake or simply choose not to complete the expression
      • the normal > prompt is shown after the .break command.
    • .save filename: saves the current REPL session to the file specified in filename
    • .load filename: runs codes within the JavaScript file specified in filename
      • The codes are executed as if each line were typed directly into REPL.
    • .clear: resets the REPL’s context object.

    2. Hello World

    1) Just print out a string “Hello World”

    2) Print “Hello” and then print “World” after two seconds.

    3) Read a file ‘test.txt’ and print out the content

    4) You can check that there is just one thread in node.js

    Although an event occurs from ‘readFile’, the corresponding handler cannot get a chance to print the file’s content and the above program cannot terminate. The reason is why there is a thread in node.js.

    Post Tagged with , ,

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

%d 블로거가 이것을 좋아합니다: