Node. js configuration development from entry to entry

Source: Internet
Author: User
Node. js configuration Development From getting started to getting started with node. JS, you are welcome to exchange and learn for the blog articles in the "getting started with node. js" series. By -- danhuang1. Windows Configuration

1, the official website (http://nodejs.org) download node Windows system corresponding (32 and 64) the latest version;

2. download and install the SDK;

3. After the installation is complete, view the node. js Startup File directory. Generally, add node.exe startup to the Windows environment variable in "C: \ Program Files \ nodejs \ node.exe.pdf;

4. Run CMD and enter the DOS command window. Enter node-version to check whether the installation is successful;

Ii. Linux Configuration

1. download the latest nodejs for Linux installation package (32-bit and 64-bit) from the official website );

2. decompress the compressed package tar-zxvf node-v0.8.8.tar.gz (different versions, different names)

3. Enter the decompressed folder CD node-v0.8.8

4. Execute make

5. Execute make install

6. After the installation is complete, run node-version to view the version information;

3. Hello World

After the installation is successful, we will compile our first node. js Program-Hello World

1. Create app. js in any folder;

2. edit the file app. js and enter the following code:

 

var http =require('http');http.createServer(function(req,res) {         res.writeHead(200, { 'Content-Type': 'text/plain' });         res.end('Hello World\n');}).listen(1337,"127.0.0.1");console.log('Serverrunning at http://127.0.0.1:1337/');

3. Enter the corresponding app. js folder and execute

Node app. js

You can view the following results:

Server running at http://127.0.0.1:1337

This indicates that an HTTP server has been started to listen to port 1337 of the local localhost.

Open the browser and enter http: // 127.0.0.1: 1337 to view the hello World

During this period, I summarized the problems encountered during the first program development.

1. Error: cannot find Module

The main cause of this problem is that app. JS is not found in the current directory, that is, the created app. JS is no longer in the current directory. You need to enter the app. js directory for execution.

2. Error: Listen eacces

The main cause of this problem is that the listening port is occupied by other applications. You can modify the current listening port. For example, you can change 1337 to 3000.

3. How to exit

The normal way to exit the listening port is Ctrl + C (Windows and Linux are consistent)

4. Error: Listen eacces occurs during the second startup.

After the listener port is started, the preceding problem does not occur when Ctrl + C is used for Windows exit. If Ctrl + Z is used for Linux exit, the port is occupied when the listener is started again, solution: Run Ctrl + C to exit. You can also run netstat-nap | grep node to view the ID of the listening port and kill the process with kill-9. Of course, we do not recommend the second method.

5. Change the node. js file and the server responds immediately.

App. js outputs Hello world, but now I want to output Hello Baby. Do I need to restart the Startup File of node. js?

6. How to Implement background running in Windows

Sometimes we want our program to run continuously and continue running when Ctrl + C exits. Here, you can use the operation in Windows to add start/B (start/B node app. JS) before running the command. Of course, if the DOS operation window is closed, the listener will be exited at the same time.

7. When using node to run JS filesThe server does not automatically monitor file changes and then restarts. You need to use the node. js module developed by GitHub users.

Common examples include node-Dev and nodemon.

The installation and configuration methods are not described in detail here. The module installation of node. js will be detailed in chapter 2. You can view the official installation and usage of the two modules above.

Nodemon: https://github.com/remy/nodemon

Node-Dev: https://github.com/fgnass/node-dev

I hope that students who encounter problems can continue to communicate with me and help new students join the node. js development team more quickly.

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.