Get the App
SLTechnology News&Howtos  ›  Development  › 

How to build a local server using node.js

Shulou Source: shulou.com Published: 2022-06-03 16:50:20 09月23日 Update

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!

Tags: Code data services databases files terminals success commands modules third parties editors inputs servers content workspaces suggestions manual more examples articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft MySQL OPPO Reno macOS