This tutorial, just to everybody throw a brick to lead jade, suitable for novice. As long as you understand JS, then you will be full stack!
1. Create an applet project
Below, we open wechat developer tools and create a small program project named test under the mini directory.
After the creation is complete, an initial code is generated as follows:
2. Create the Express project
Create a new server directory in the same directory as the mini project directory above and open the command window in that directory
Execute the following command from the command window to create the project
#Initialize the project and press Enter all the way
npm init
#Install express
npm install express
#To generate the Express project template, select Y
npx express-generator
#Installation project dependencies
npm install
Copy the code
When completed, the project code is structured as follows
Currently, we only need to write interfaces, so we only need to operate in routes, app.js. In this case, run the NPM run start command to start the server. If http://localhost:3000 is accessed, the following page is displayed.
3. Install mysql or mongodb
You can choose either mysql or mongodb without affecting the basic teaching
Mysql installation: If you want to use mysql5.7 or 8.0, you are advised to install the mysql5.7 or 8.0 password. So, if you don’t want to mess around, you can go with 5.7. Once installed, we can connect to it through the database visualization tool, as shown below
After the connection is successful, we create a new test database, the other two options, the default is ok
Mongodb installation: I will not write here, and mysql similar steps.
4. Create the table Anime
As shown, create a simple table to store animation information, whereid
Make sure you set it to auto-increment
5. Background interface
Under the newly created project directory server, install access-DB and dotenv
npm install access-db
npm install dotenv
Copy the code
1). Introduce dotenv at the front of app.js as follows:
require('dotenv').config()
var createError = require('http-errors');
var express = require('express'); .Copy the code
2). In the root directory of the project, create a. Env and configure the data thinking information. If you are mongodb, configure mongodb information. For details, see the access-DB documentation
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
MYSQL_PORT=3306
MYSQL_DATABASE=test // Which database to use
Copy the code
3). In the /routes directory, create a new animo.js route with the following code:
var express = require('express');
var routerAnime = express.Router();
/* GET users listing. */
routerAnime.get('/'.function(req, res, next) {
res.send('anime api');
});
module.exports = routerAnime;
Copy the code
4). Import the above route in app.js:
.var animeRouter = require('./routes/anime')
var app = express()
...
app.use('/anime', animeRouter)
Copy the code
So it’s going to look something like this
This time, you start the project, and then open http://localhost:3000/anime in a browser, will appear the following information. At this point, a simple interface is complete.