Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

Time:2021-8-20

This tutorial is just for you to attract jade, suitable for novices.As long as you know JS, you will be the whole stack!

1. Create a new applet project

As follows, we open the wechat developer tool and create a tool namedtestApplet project, project inminiUnder the directory.

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

After creation, an initial code will be generated as follows:

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

2. Create a new Express Project

Applet project directory aboveminiCreate a new one in the same level directory ofserverDirectory, and in this directory, open the command window

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

In the command window, execute the following command to create a project

#Initialize the project and press enter all the time
npm init

#Install Express
npm install express

#Generate the express project template and select y
npx express-generator

#Install project dependencies
npm install

After completion, the project code is structured as follows

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

At present, we only need to write interfaces, so we only need toroutesapp.jsOperate inside.
At this point, executenpm run startStart the server and accesshttp://localhost:3000The following interface appears, indicating success.

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

3. Install MySQL or mongodb

You can choose either MySQL or mongodb without affecting the later basic teaching

MySQL installation: if you just want to facilitate learning, it is recommended to install MySQL version 5.7 and version 8.0 password types. At present, node does not support it, and you need to change the configuration. So, if you don’t want to toss, you can choose 5.7. After installation, we can connect it through the database visualization tool, as follows

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

After the connection is successful, we will create a new onetestDatabase, the other two options can be selected by default

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

Mongodb installation: I won’t write here. It’s similar to MySQL.

4. Create table anime

As shown in the figure, create a simple table for storing animation information, whereidBe sure to set it to auto increment
Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

5. Background interface

New project directory created beforeserverNext, installaccess-dbanddotenv

npm install access-db
npm install dotenv

1) . inapp.jsFirst introduction ofdotenv, as follows:

require('dotenv').config()
var createError = require('http-errors');
var express = require('express');
...

2) . in the project root directory, create a new.envAnd configure data and information. If you are mongodb, configure mongodb. For details, see the access DB documentation

MYSQL_HOST=localhost 
MYSQL_USER=root
MYSQL_PASSWORD=123456
MYSQL_PORT=3306
MYSQL_ Database = Test // which database do you want to use

3) . in/routesDirectory, newanime.jsThe routing code is as follows:

var express = require('express');
var routerAnime = express.Router();

/* GET users listing. */
routerAnime.get('/', function(req, res, next) {
  res.send('anime api');
});

module.exports = routerAnime;

4) . inapp.jsThe above route is introduced into the:

...
var animeRouter = require('./routes/anime')

var app = express()

...
app.use('/anime', animeRouter)

That’s about it in the end

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)

At this time, you start the project and open it in the browserhttp://localhost:3000/anime, the following message will appear. At this point, a simple interface is completed.

Node.js + Express + access DB to easily realize the full stack development of small programs (Part I)