Home > Web Front-end > JS Tutorial > A brief discussion on how to build a local server using node.js

A brief discussion on how to build a local server using node.js

青灯夜游
Release: 2021-09-09 11:24:49
forward
3430 people have browsed it

Using node.jsHow to set up a local server? The following article will introduce to you how to build a local server with node.js. I hope it will be helpful to you!

A brief discussion on how to build a local server using node.js

node.js is a back-end language based on JavaScript. Front-end friends can get started quickly and build a local server by themselves. Let’s take a look at how to do it~

[Recommended study: "nodejs Tutorial"]


Note: This article You need to understand the add, delete, modify, and query commands of the MySQL database, and you need to manually create a new warehouse and data table

1. Node installation and simple use

1. Download

official website: node

  • node official website download node.js and install it .

A brief discussion on how to build a local server using node.js

  • After successful installation, open any terminal window, here use the cmd window (enter cmd after win r and press Enter)

A brief discussion on how to build a local server using node.js

  • Enter node -v in the terminal window. If the node version number appears, the installation is successful

A brief discussion on how to build a local server using node.js

2. Simple use

Note: Use VSCode editor demonstration here, file name Can be customized, it is recommended to use an English name!

  • Create a new code folder and open it with the code editor

  • In the workspace, right-click and select Open

A brief discussion on how to build a local server using node.js

  • in the integrated terminal and enter npm init -y to quickly execute npm Initialization

  • After the initialization is completed, the file package.json will appear in the workspace, where the downloaded third-party modules

will be recorded.

A brief discussion on how to build a local server using node.js

  • If you are new to npm, it is recommended to execute the following command and use Taobao mirror to download, which 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 modules

    npm install express mysql

A brief discussion on how to build a local server using node.js

    ##After successful download (as shown in the picture below)

A brief discussion on how to build a local server using node.js

2. Code Demonstration

##1. Connect to the databaseThe code is as follows (example):

New

db.js

In order for the code structure to be clear and reusable, select a new file here to connect to the mysql database<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">    //导出    module.exports = (sql,callback) =&gt; {        const mysql = require('mysql')        const conn = mysql.createConnection({            host:'localhost',            // user、password需手动添加,与数据库保持一致            user:'',            password:'',            database:'数据库名'         })        // 建立连接         conn.connect()         conn.query(sql,callback)        // 断开连接         conn.end()     }复制代码</pre><div class="contentsignin">Copy after login</div></div>

2. Create a new local serviceThe code is as follows (example):

New

index.js

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> //首先加载express    const express = require('express')    const app = express()    //端口号    const port = 3000    //引入自定义的mysql文件    const db = require('./db.js')         //这里仅列举发送GET请求     app.get('url',(req,res) =&gt;{         db('select * from 表名', (err,result) =&gt; {            if(err) throw err             res.send(result)         })     })          app.listen(port,() =&gt; console.log('server is start,port is', port))复制代码</pre><div class="contentsignin">Copy after login</div></div>

3. Test local service

Code editor run
    index.js

A brief discussion on how to build a local server using node.js

Use
    ApiPost
  • Software to test local services
  • ##127.0.0.1
or

localhostAll are local addresses

A brief discussion on how to build a local server using node.js

Summary

This article does not explain how to create a new database, if you need a demonstration , leave a message in the comment area and tell me~

For more programming-related knowledge, please visit:

Programming Video

! !

The above is the detailed content of A brief discussion on how to build a local server using node.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template