Start programming with node. js

Source: Internet
Author: User

The beginning of all the programs is Hello world haha

Open Text editor inside write:console.log (' Hello World ');

Save As Helloworld.js , open the terminal, enter directory where helloworld.js is located

Execution: node Helloworld.js

If everything is OK, you will see the output in the terminal Hello World

It's too easy, but it's a little technical, but it has to be nagging.

console Yes node.js console.log Console.error console.log c in the language printf %d , %s

    Consolelog.js
    Console.log ('%s:%d ', ' Hello ', 25);

the output is hello:25

node mode 8 repl Read-eval-print loop python

Tao runs without parameters under terminal python command or use python IDLE open shell node.js node javascript interactive shell

Enter REPL mode, you will receive a " > prompt prompts you to enter a command

Node network connection and PHP comparison:

Start using Nodejs to build the server now:

var http = require (' http ');

Http.createserver (function (req, res) {Res.writehead ($, {' Content-type ': ' text/html '}); Res.write ('

Res.end (' <p>hello world</p> ');

}). Listen (3000);

Console.log ("HTTP server is listening at Port 3000.");


Next Run node App.js command, open browser access http://127.0.0.1:3000 can see Hell World results on the web

The red script above executes and does not end with the print playing Hello world. You need to press CTRL + C to terminate the run.

because Listen function creates an event listener that makes The node. js process does not exit the event loop

Tip: Use Supervisor

If you have PHP development experience, will be accustomed to modify PHP The script directly refreshes the browser to observe the results while you are developing node. js implementation of HTTP application will find that no matter which part of the code you have modified, you must terminate node. js re-run will not work. This is because node. js The script file is parsed only when it is first referenced to a part, and will be accessed directly in the future, avoiding repeated loading, and PHP The script is always re-read and parsed (if there is no dedicated optimization configuration). node. jsThis design, while improving performance, is not conducive to the development of debugging, because we always want to change in the development process immediately see the effect, rather than each time to terminate the process and restart.

Supervisor can help you implement this function, it will monitor your code changes, and automatically restart node. js . The easy way to use it is to first use npm to install Supervisor:

    $ NPM install-g Supervisor

Next, use the Supervisor Command Start App.js : $ supervisor App.js

Run:










Start programming with node. js

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.