In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to build a local server using node.js. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Note: this article needs to understand the add, delete, modify and query commands of MySQL database, and manually create new warehouses and data tables.
I. installation and simple use of node
1. download
Official website: node
Download and install node.js on the node official website.
After the installation is successful, open any terminal window and use the cmd window here (enter cmd after win + r)
Enter node-v in the terminal window, and the node version number indicates that the installation is successful.
two。 Easy to use
Note: here use the VSCode editor to demonstrate, the file name can be customized, it is recommended to use the English name!
Create a new code folder and open it using the code editor
In the workspace, right-click and select to open in the integrated terminal
Input npm init-y into the integrated terminal for fast initialization of npm
After initialization, the package.json file will appear in the workspace, where the downloaded third-party modules will be recorded.
For those who contact npm for the first time, it is recommended to execute the following command. Using Taobao's image download will speed up the download speed of third-party modules.
Npm config set registry https://registry.npm.taobao.org
Next, start executing the command to download the required third-party module
Npm install express mysql
After a successful download (as shown in the following figure)
Second, code demonstration
1. Connect to the database
The code is as follows (example):
New db.js in order to make the code structure clear and reusable, we choose to create a new file and connect to the mysql database.
/ / Export module.exports = (sql,callback) = > {const mysql = require ('mysql') const conn = mysql.createConnection ({host:'localhost', / / user, password need to be manually added to keep consistent with the database user:'', password:'' Database:' database name'}) / / establish a connection conn.connect () conn.query (sql,callback) / / disconnect conn.end ()} copy code
two。 New local service
The code is as follows (example):
Create a new index.js
/ / first load express const express = require ('express') const app = express () / / port number const port = 3000 / / introduce a custom mysql file const db = require ('. / db.js') / / here only enumerate the sending GET request app.get ('url', (req,res) = > {db (' select * from table name', (err) Result) = > {if (err) throw err res.send (result)})}) app.listen (port, () = > console.log ('server is start,port is', port)) copy code
3. Test local services
The code editor runs index.js
Use ApiPost software to test local services
127.0.0.1 or localhost is the local address
Thank you for reading! This is the end of the article on "how to build a local server using node.js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.