Nodejs Quick Start

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>Directory:</p></p> <ul> <ul> <li>Write the first Node. JS program;</li> <li>Asynchronous I/O and event loops;</li> <li>Modules and packages;</li> <li>Debugging.</li> </ul> </ul><strong><strong>1. Writing the first Node. JS program;</strong></strong><p><p>Node. JS has a deep, open source pedigree that was born in--github, a site hosting many of the best open source Projects. Like most Open-source software, it was initiated by a hacker and then attracted a small number of enthusiasts to contribute to the Code. At first it was unknown, spread by word of mouth, until one day was exposed by a hacker media, into the field of vision, then some visionary companies to provide business support, so that it gradually grow and develop.</p></p><p><p>Programming with Node. JS is a pleasant thing to do because you will begin to write code in a Hacker's mind and Style. You'll find that language like this is easy to get started with, you can quickly learn the details of it, and then master it.</p></p>1.1helloworld<p><p>ok, Let's start implementing the first Node. JS Program. Open your favorite text editor, where you enter:</p></p><pre><pre>Console.log (<span style="color: #800000;">'</span><span style="color: #800000;">Hello</span>World<span style="color: #800000;">'</span>);</pre></pre><p><p>Save the file as helloworld.js, open the terminal, enter the directory where Helloworld.js is located, and execute the following command:</p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;">Node Helloworld.js</pre></pre><p><p>If everything is ok, you will see the output Hello world in the Terminal.</p></p><p><p></p></p><p><p>Console is a console object provided by Node. JS that contains operations written to standard output, such as console.log, console.error, and so On. Console.log is our most commonly used output instruction, it is similar to the function of printf in C, can accept any number of parameters, support%d,%s variable reference,</p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;">Consolelog.jsconsole.log ('%s:%d ', ' Hello ', 25);</pre></pre>1.2 Nodejs command-line tools<p><p>In the previous Hello world example, we used the node command in the command line and entered Node--help to see the detailed help information:</p></p><p><p></p></p><p><p>It shows the use of node, and the basic way to run the Node. JS program is to execute node script.js, where script.js is the file name of the Script.</p></p><p><p>In addition to running script files directly, the method of using Node--help shows another way to output HelloWorld:</p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;">$ node-e "console.log (' Hello World '); Hello World</pre></pre><p><p>We can execute the statement that we want to execute as a node-e parameter directly.</p></p><p><p><strong>Using Node's REPL mode:</strong></p></p><p><p>REPL (read-eval-print loop), which is the Input-evaluation-output loop. If you have used python, you will know that running a parameterless Python command under the terminal or using the Python open shell will allow you to enter an immediate evaluation Environment. Node. JS also has the capability to run a parameterless node that will launch a JavaScript interactive shell:</p></p><p><p>  </p></p><p><p>1.3 Setting up an HTTP server</p></p><p><p>Node. JS is the platform for the network, but it is very different from ASP and PHP. If you have PHP development experience, you will know to configure a powerful and complex HTTP server before running PHP successfully, such as Apache, IIS or Nginx, also need to configure PHP as an HTTP server module, or use the FASTCGI protocol to invoke PHP interpretation Manager This architecture is "browser?" HTTP server? PHP interpreter, and Node. JS uses a different organization,</p></p><p><p></p></p><p><p>As we can see, Node. js leaves the "http server" layer out and targets the browser user directly. This architecture is, in a sense, Disruptive.</p></p><p><p>ok, back to the chase, let's create an HTTP server. Create a file named App.js with the Following:</p></p><pre class="brush:javascript;gutter:true;"><pre class="brush:javascript;gutter:true;">App.jsvar http = require (' http '); http.createserver (function (req, res) {res.writehead ($, {' content-type ': ' text/ HTML '}); res.write ('

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.