Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Node.js

Shulou Source: shulou.com Published: 2022-06-01 06:36:20 09月17日 Update

This article mainly introduces how to use Node.js, the introduction in the article is very detailed, has a certain reference value, interested friends must read!

1. Node.js1. Basic overview

Node.js is a platform that allows JavaScript to run on the server side. It is a platform for real-time Web application development. From the beginning of its birth, it has fully considered the scalability of the architecture under the requirements of real-time response and very large-scale data. This makes it abandon the design idea that the traditional platform relies on multithreading to achieve high concurrency, and adopts single-threaded, asynchronous Imax O, event-driven programming mode.

Node.js has powerful and flexible package manager (node package manager, npm). At present, there are tens of thousands of third-party modules, including website development framework, MySQL, PostgreSQL, MongoDB database interface, template language parsing, CSS generation tools, mail, encryption, graphics, debugging support, and even graphical user interface and operating system API tools.

Node.js can provide services to users as a server. Compared with PHP, Python and Ruby on Rails, it skips HTTP servers such as Apache and Nginx, and directly faces front-end development. Many of the design concepts of Node.js are very different from classic architectures such as LAMP and provide powerful scalability.

Second, install node.js

Download address: https://nodejs.org/zh-cn/download/

Let's take specific steps, Baidu.

Second, set up node server

First install the global vue scaffolding vue-cli command line execution: npm install-g vue-cli / / add-g is installed globally to create a server.js file in the root directory of the project

The code is as follows:

Var http = require ('http'); http.createServer (function (request, response) {/ / send HTTP header / / HTTP status value: 200: OK / / content type: text/plain response.writeHead (200, {' Content-Type': 'text/plain'})

/ / send the response data "Hello node.js" response.end ('Hello node.js\ n');}) .reply (8888); / / the terminal prints the following information console.log ('Server running at http://127.0.0.1:8888/');)

Start server.js

D:\ vue-project\ demo > node server.jsServer running at http://127.0.0.1:8888/

Just visit http://127.0.0.1:8888/ on the web page.

Third, connect to mysql database

Use Taobao image

1. Install Taobao image npm install-g cnpm-- registry= https://registry.npm.taobao.org

two。 Install mysql cnpm install mysql

Create a test.js file

Var mysql = require ('mysql'); var connection = mysql.createConnection ({host:' localhost', / / local database user: 'root', / / username password:' mysql', / / password port: '3306', / / port number database: 'cgs' / / database name})

Connection.connect (); / / Connect var sql = 'SELECT * FROM users'; / / sql statement

Connection.query (sql, function (err, result) {if (err) {/ / error message console.log ('[SELECT ERROR] -', err.message); return;}

Console.log ('--SELECT--'); console.log (result) Console.log ('- -\ n\ n');})

Connection.end (); / / connection ends

Start test.js node test.js

This successfully connects to the database.

The above is all the contents of this article "how to use Node.js". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Data database service server content platform user development design power information global graphics real-time tools files architecture articles threads mirrors Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi macOS Linux Huawei