Paip. Build a connection to mysql in the node js environment of the best scripting language

Source: Internet
Author: User
Tags stack trace

Paip. Build a connection to mysql in the node js environment of the best scripting language

#=== Download node... take eleven exe... Easy... 1

#0 ---- Hello world. js 2

# --------- Create and use the module... 2

1. ## -- Call (Load file module --- normal file module 3

2.) 3

3. ##--- load the built-in Module 3

4. ##---- 3. Load file directory Module 3

5. ## -- 4. Load Module 4 in node_modules

---- Third-party module download and installation 4

# --------- Call mysql 4

----- Install npm 7

# ------------- Debug node-inspector/eclipse/webstorm 7

---- Solve some errors 7

--- Node. js, advantage 8

6. Do not specially view ide... hh. This is better than php and python. 8

7. node. js, high concurrency (the most important advantage) (ALL asynchronous calls) 8

8. Unified Front-end pull... 9

9. High development efficiency, which is twice that of java, similar to php. 9

10. All in all, NodeJS is applicable to scenarios with high concurrency, I/O intensive, and a small amount of business logic. 9

11. JavaScript is also easy to learn 9

--- Node js disadvantage 9

12. The standard library is too small. Download the One-watt library. 9

13. There are too few materials and the ecosystem has not yet been established

14. No Default debugging supported by ide 9

15. Disadvantages: 1. Not Suitable for CPU-intensive applications; 9

. Only single-core CPU is supported, and CPU 9 cannot be fully utilized

17. 3. low reliability. Once the code crashes at a specific stage, the entire system crashes. 9

. The quality of open-source component libraries is uneven, fast updates, and backward incompatible 10

19. 5. Debug is inconvenient. The error is not reported by stack trace 10.

-- Ide webstorm 1. IDE: Webstorm, no other. 10

Asynchronous Process Control: Promise, Q. js 10

V. Front-end framework: Angular. js 10

Conclusion: 10

Refer to 10

#==== Download node... take eleven exe... easy ..

I selected the. EXE file. In addition to copying node.exe to the C: \ Program File (x86) \ directory and modifying the system Path, the. msiinstallation File has no other functions.

For ease, do not add a node to windows. Do not add it to a path ..

Node version

C: \> node-v

V0.10.28

#0 ---- Hello world. js

Hello. js file, and then enter:

Var sys = require ("sys ");

Sys. puts ("Hello world ");

Then, enter the node hello. js command in the nameboard to view the output result Hello world.

Author old wow's paw Attilax iron, EMAIL: 1466519819@qq.com
Reprinted please indicate Source: http://blog.csdn.net/attilax

# --------- Create and use the module ..

Each module exposes a public API. So that developers can import data. For example

Var mod = require ('module _ name ')

# -- Mode1.js

Function meth1 (name ){

Console. log ('hello, '+ name );

}

Exports. meth1 = meth1;

1. ## -- Call (Load file module --- normal file module 2 .)

Note that the extension ". js" is ignored here, and the extension can also be used, and the absolute path can also be used.

Var h = require ('./mode1 ');

H. meth1 ('snandy ');

3. ##--- load the built-in Module

Node's built-in modules are compiled in binary format. When referenced, they directly use the name rather than the file path. When the third-party module and the built-in module have the same name, the built-in module will overwrite the third-party module with the same name. Therefore, do not name the module with the same name as the built-in module. For example, obtain an http module.

1

Var http = require ('http ')

4. ##---- 3. Load the file directory Module

You can directly request a directory. assume there is a directory named folder, as shown in figure

1

Var myMod = require ('./folder ')

In this case, Node searches for the entire folder directory. Node assumes that folder is a package and tries to find the package definition file package. json. If the folder directory does not contain the package. json file, Node will assume that the default main file is index. js, and index. js will be loaded. If index. js does not exist, loading will fail.

5. ## -- 4. Load the module in node_modules

If the module name is neither a path nor a built-in module, Node will try to search in the node_modules folder of the current directory. If no Node is found in the node_modules directory of the current directory, the Node will search from the node_modules directory of the parent directory, so that it will recursively go down until the root directory.

Don't worry. The npm Command makes it easy for us to install, uninstall, and update the node_modules directory.

---- Download and install third-party modules

Normal use of npm playing can be downloaded. Things cannot be performed. You have to download them manually ..

Node website module> https://www.npmjs.org/> search mysql> https://github.com/felixge/node-mysql> download zip format ..

When I search mysql, I can see that one watt of dependent library is about to be downloaded ..

Installation: upzip> renamed to module name...> put "C: \ Program Files \ nod

Ejs \ node_modules. % root %/node_modules.

Korean new dog processmonitor view to hanging.

# --------- Call mysql

Node website module> https://www.npmjs.org/> search mysql> https://github.com/felixge/node-mysql> download zip format ..

When I search mysql, I can see that one watt of dependent library is about to be downloaded ..

/// Bignumber. js core-util-is inherits isarray readable-stream require-all

Var sys = require ("sys ");

Sys. puts ("Hello world ");

Sys. puts ("Hello aaa ");

Sys. puts ("Hello bbbb ");

Var h = require ('./mode1 ');

H. meth1 ('snandy ');

// Require ('C: \ node_modules ')

// Load the mysql module

Var mysql = require ("mysql ");

Var dataBaseLinkInfo = {

"Host": '2017. 0.0.1 ',

"Database": 'dbc8 ',

"Port": 3306,

"User": 'root ',

"Password ":""

}

Var connection = mysql. createConnection (dataBaseLinkInfo );

Connection. connect ();

Connection. query ('select * FROM grejx_def limit 2', function selectCb (err, rows, fields ){

If (err ){

Throw err;

}

Console. log (rows );

Sys. puts ("--------------------------------------- Hello bbbb ");

// Console. log (fields );

})

Connection. end ();

----- Install npm

Npm is used to download the library... it has reached the msi... it is not the same as the 10 Russian, and the other one is the same ..

Go to a npm directory named 10% node_install %/node_modules ..

The npm in the call is % node_install %/npm. bat.

Actually executed 10 ::

"C: \ Program Files \ nodejs \ node.exe" "C: \ Program Files \ nod

Ejs \ node_modules \ npm \ bin \ npm-cli.js "" install "" node-inspector"

The module cannot be automatically installed even though it is in Russia. If there is an error, You can manually install the module ..

# ------------- Debug node-inspector/eclipse/webstorm

Node. js itself supports debugging (gdb is also not easy to use). You can add a breakpoint by adding the debugger command before the statement.

Add the debug option when starting the service.

Node debug index. js

---- Solve some errors

2. During execution, the Error "Error: Cannot find module 'mysql'" is reported '"

3. Solution: Add NODE_PATH to the environment variable. The path is C: \ Users \ chezg \ node_modules, that is, the installation path of the mysql module.

--- Node. js, advantage 6. do not specially ide view .. hh, this is better than php and python. 7. node. js, high concurrency (the most important advantage) (ALL asynchronous calls)

Single Process and single thread (message-based, erlang is also used)

Summarize how NodeJS solves the problem of concurrent connections: Change the connection method to the server, and each connection is initiated (emit) an Event that runs in the NodeJS engine process ), put it into the event queue, instead of generating a new OS thread for each connection (and allocate some supporting memory for it)

Turn: Why does PayPal migrate data from Java to Node. js? The performance doubles and the file code is reduced by 44%

In this figure, you can see the advantages of the node. js application: the number of requests per second is twice that of Java. But what's more interesting is that we only use single-core nodes to compare with the 5-core Java, and we hope to inherit and expand the advantages of node. js in the future.
Rendering the same page saves node. js 35% of the time. That is to say, each page saves 200 seconds, and users can clearly feel the difference.

8. uniform pull at the front-end background .. 9. the development efficiency is high, which is twice that of java. It is similar to php. 10. all in all, NodeJS is applicable to scenarios with high concurrency, I/O intensive, and a small amount of business logic. 11. javaScript is also easy to learn-disadvantages 12 of node js. the standard library is too small. download the yiwa library. 13. there are too few materials, and the ecosystem has not yet been established. 14. no Default debugging ide support

These two points are too bad than php ..

15. Disadvantages: 1. Not Suitable for CPU-intensive applications;

CPU-intensive applications pose major challenges to Node: Due to the single-thread JavaScript, if a computing task runs for a long time (such as a large loop), the CPU time slice cannot be released, the subsequent I/O cannot be initiated;

Solution: Decomposition of large computing tasks into multiple small tasks allows the computing to be released in a timely manner without blocking the initiation of I/O calls;

. Only single-core CPU is supported, and CPU cannot be fully utilized. 3. low reliability. Once the code crashes at a certain stage, the entire system crashes.

Cause: single process, single thread

Solution: (1) Nnigx reverse proxy, Server Load balancer, enable multiple processes, and bind multiple ports;

(2) enable multiple processes to listen to the same port and use the cluster module;

. The quality of open-source component libraries is uneven, fast updates, and backward incompatible. 19. 5. Debug is inconvenient, and the error is not stack trace -- ide webstorm 1. IDE: Webstorm, no other.

Some people say that the debugging cost is high. It depends on the development tool you are using. We recommend webstorm to perform one-step debugging on the Node. js program.

Asynchronous Process Control: Promise, Q. js

There are N types of tokens for the beans. Therefore, experts tell us that in addition to callback functions, asynchronous processes also include event methods, subscription and publishing modes, and Promise. My answer is only one, Promise, in Q. js.

V. Front-end framework: Angular. js

Conclusion:

The current status. node. js cannot replace php... php or the king of the web.

Reference

Reference

Nodejs debugging methods, node-inspector can focus on-terrychinaz's column-blog channel-CSDN.NET.htm

Module loading method and mechanism of Node. js-snandy-blog .htm

Node. js Cannot find module 'xxx'-freeman983-iteyetechnical website .htm

Problems encountered when impt (nodejs) nodejs connects to mysql and solutions-bimarcher's column-blog channel-CSDN.NET.htm

Impt nodejs debugging methods, node-inspector can focus on-terrychinaz's column-blog channel-CSDN.NET.htm

Related Article

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.