This tutorial is just for everyone to learn from, suitable for novices. As long as you understand js, then you will be full stack!
1. Create a new mini program project
As follows, we open the WeChat developer tool and create a test
, which is under the mini
directory.
After the creation is complete, an initial code will be generated, as follows:
2. New express project
In the same level directory of the above applet project directory mini
server
directory 060e41497ac8e2, and in this directory, open the command window
Execute the following commands in the command window to create the project
# 初始化项目,然后一直回车就可以了
npm init
# 安装express
npm install express
# 生成express项目模板,选择y
npx express-generator
# 安装项目依赖
npm install
After completion, the project code is structured as follows
Among them, we only need to write the interface at present, so we only need to operate in routes
and app.js
.
At this point, execute npm run start
start the server, access http://localhost:3000
following interface appears, indicating success.
3. Install mysql or mongodb
mysql or mongodb, you can choose any one, it will not affect the basic teaching
MySQL installation: If you are just for the convenience of learning, it is recommended to install mysql5.7 version, 8.0 version of the password type, currently node does not support, you need to change the configuration. So, if you don't want to toss, you can choose 5.7. After installation, we can connect to it through the database visualization tool, as follows
After the connection is successful, we create a new test
database, the other two options, just default
mongodb installation: I will not write here, the steps are similar to mysql.
4. Create table anime
As shown in the figure, create a simple table to store animation information, in which id
must be set 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
1) In app.js
foremost introduced dotenv
, as follows:
require('dotenv').config()
var createError = require('http-errors');
var express = require('express');
...
2). In the project root directory, create a new .env
, and configure the data and information. If you are mongodb, just configure mongodb. For detailed configuration, please see access-db document
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
MYSQL_PORT=3306
MYSQL_DATABASE=test //要使用的哪个数据库
3). In the /routes
directory, create a new anime.js
route, the 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). app.js
above route in 060e41497acb39:
...
var animeRouter = require('./routes/anime')
var app = express()
...
app.use('/anime', animeRouter)
It's probably like this in the end
At this time, if you start the project and open http://localhost:3000/anime
in the browser, the following message will appear. At this point, a simple interface is complete.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。