Node quickly builds the backend implementation steps

Node quickly builds the backend implementation steps

1. First install node, express, express-generator (the 4.x version separates the generator so it needs to be installed separately)

2. Enter the project file and enter the command express 項目名, npm i install the corresponding package, then the simple backend framework will be set up for you. The default port is 3000. Modify www under bin

I usually rewrite app.js and then delete the bin file, because the simpler the better.

 var http = require('http')
var server = http.createServer(app)
//Omit the middle and then delete the direct listening in the final export app server.listen('3030',()=>{console.log('Server started successfully');})

Finally, modify package.json. Due to everyone's habits, I am used to enabling the dev startup in the scripts.

 "scripts": { "dev": "nodemon ./app.js" },

Note that I installed nodemon here and used hot module replacement, which is real-time refresh.

2. Install the mysql database

Then test it in the index

I won’t talk about the detailed database operations.

3. The following is the secondary encapsulation of routing

First, create a file app/index.js for routing requests outside

 //This is to handle the execution of database statements const { exec } = require('../unil/db')
/* GET home page. */
init=(req,res)=>{
  exec('select * from goods_type_info where 1=1', [], (err, result) => {
    if (err) {
      console.log('Service link error');
    } else {
      res.send({code:200000,data:result})
    }
  })
}

module.exports = {init};

//Then call it in the required file let getDate=require('../app/index')
/* GET home page. */
router.get('/',getDate.init); //Which route executes which statement? This is the first one to process the route. The second one is the method of route execution. If the route is nested, it needs to be spliced.

The classification here can be more detailed. For example, all the routes used by which page can be separated and then imported uniformly. In addition, the template created by express quickly does not need to import body-parser The first one is because it is deprecated, and the second one is because it uses app.use(express.json());app.use(express.urlencoded({ extended: false })); instead.

Here, the front end remembers that the request header initiated is in this format, otherwise there will be problems with the data format received on the back end. Different formats have different writing methods. For details, see Baidu Content-Type

In this way, you can quickly build a simple background framework

Here I would like to add a problem I encountered. If you build it locally, you must write the port number when opening the front end, otherwise it is easy to cause false cross-domain, causing the front end to request, the back end to save data, and you cannot get the session when you request the second time.

This is the end of this article about node quick background construction. For more relevant node quick background construction content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Node.js builds small program background service

<<:  The Complete List of MIME Types

>>:  Html tips to make your code semantic

Blog    

Recommend

A tutorial on how to install, use, and automatically compile TypeScript

1. Introduction to TypeScript The previous articl...

Mysql string interception and obtaining data in the specified string

Preface: I encountered a requirement to extract s...

Detailed explanation of the new CSS display:box property

1. display:box; Setting this property on an eleme...

Detailed explanation of JavaScript axios installation and packaging case

1. Download the axios plugin cnpm install axios -...

Detailed examples of the difference between methods watch and computed in Vue.js

Table of contents Preface introduce 1. Mechanism ...

Why does using limit in MySQL affect performance?

First, let me explain the version of MySQL: mysql...

What is COLLATE in MYSQL?

Preface Execute the show create table <tablena...

iframe src assignment problem (server side)

I encountered this problem today. I reassigned the...

JS implements WeChat's "shit bombing" function

Hello everyone, I am Qiufeng. Recently, WeChat ha...

Creation, constraints and deletion of foreign keys in MySQL

Preface After MySQL version 3.23.44, InnoDB engin...