Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Node.js

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report