Detailed explanation of basic nodejs operation methods
The previous project used PHP for high-frequency collection and settlement, which greatly reduced the efficiency of our PHP, and PHP blocking prevented our web pages from running normally. So find a language that can replace PHP for database operations and collection, and perfectly integrate with PHP.
Node.js is a JavaScript runtime environment based on the Chrome V8 engine. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient. Node.js's package manager npm is the world's largest open source library ecosystem.
First introduction to nodejs
nodejs, just like our PHP composer, you can use the npm command to download nodejs related plug-ins.
You can only use the front-end javascript to operate basic functions, and the cost of learning is greatly reduced.
Normal operation
nodejs link database
At this time we will rely on npm to download the mysql module
Switch to our project, npm install mysql -save
Create a file that runs sql mysql.js
1 2 3 4 5 6 7 8 9 10 |
|
Run the js. At this time, most functions can be implemented, including regular settlement functions, sharing the pressure of php, including collecting data
Make an example of nodejs collecting 1680210 lottery tickets and storing them in the database
Create pacong.js
1 2 3 4 5 6 7 8 |
|
where var iconv = require('iconv-lite'); This module needs to be downloaded and introduced by npm to solve the problem The Chinese garbled code problem
The result is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
The cheerio module is introduced to collect and process more complex data, and the crawler website
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
combined with the operation of nodejs sql can insert the data into the database. Or other related operations
Use nodejs to make web pages and implement routing functions
Use npm to introduce express
Create a web.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
We visit 127.0 .0.1:8081 You can access the response page, or operate
The packaging of related operations of the database
Click to download the package file
Look first How to use
1 2 3 4 5 |
|
Take select as an example
Create sql:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Execute sql:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
How to reference a packaged js file
1 |
|
Summary: Database operation is an asynchronous process. It can greatly improve the work efficiency of nodejs, and at the same time
analyze an asynchronous example
1 2 3 4 5 6 7 |
|
How nodejs sends data to php through http
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
php side receives the nodejs delivery Information
1 2 3 |
|
Summary
Nodejs is still the tip of the iceberg, and there is a lot to learn.
nidejs collection Api demo
nodejs master blog tutorial
The above is the detailed content of Detailed explanation of basic nodejs operation methods. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Yes, Node.js can be used for front-end development, and key advantages include high performance, rich ecosystem, and cross-platform compatibility. Considerations to consider are learning curve, tool support, and small community size.

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.
