Make writing a habit together! This is the 8th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.
Scaffold back end project created
Egg.js project initialization
Create a folder HZW -dev-cli-server and go to the folder
Run the following command to use the default values.
npm init egg --type=simple
npm i
npm run dev
Copy the code
It will then generate a link for you
The following screen will appear when you open it, indicating that an egg program has been successfully started.
Add the API
The new app/controller/project. Js
// app\controller\project.js
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = [{ a: 1.b: 2}]; }}module.exports = HomeController;
Copy the code
Modify the app \ router. Js
// app\router.js
'use strict';
/ * * *@param {Egg.Application} app - egg application
*/
module.exports = app= > {
const { router, controller } = app;
router.get('/', controller.home.index);
router.get('/project', controller.project.index);
};
Copy the code
Go to http://127.0.0.1:7001/project
An API has been added.
Then clean up the project, get rid of the stuff, and change the code a little bit.
// app\controller\project.js
const Controller = require('egg').Controller;
class HomeController extends Controller {
async getTemplate() {
const { ctx } = this;
ctx.body = 'get template'; }}module.exports = HomeController;
Copy the code
// app\router.js
module.exports = app= > {
const { router, controller } = app;
router.get('/project/gettemplate', controller.project.getTemplate);
};
Copy the code
Modify the Hosts
You can use the switchHosts software to manage local hosts
Just add this line and you can use the domain name to access the local development environment
Now we go to http://www.duwanyu.com:7001/project/gettemplate
The result is the same
How to use mongodb
Local mongodb Installation
Download from 👉👉
Download all the default installation can be.
Find the bin directory and run mongo.exe. If the following screen appears, the startup is successful.
Enter http://localhost:27017/ in the browser
The following interface appears:
Mongo. exe fails to open for the second time, create a new data/db directory in the root directory of disk D, then run.\ mongod-dbpath D :\data\db, the problem is solved.
mongodb compass
Website after download and install, by directing: / / 127.0.0.1:27017 connected to the local database
Add a database using the visualization tool, create a collection, and then add a piece of data.
You can see that the database and collection are already created, and add data using the Add Data button
A single piece of data is inserted.
Command line operation
I added mongod. Exe directly to Windows Terminal, so that you can quickly use the command line to operate the database.
- Show DBS: Lists all databases.
- use hzw-cli-dev: switch to the
hzw-cli-dev
The database - Db: indicates the current database
- Show Collections: Lists all collections under the current database
- Db.template.find () : Queries data in the collection
Egg.js connects to the local mongodb database
Import the @pick-star/cli-mongodb tool and modify the corresponding code
npm i -S @pick-star/cli-mongodb
Copy the code
app\utils\mongodb.js
const Mongodb = require('@pick-star/cli-mongodb');
const { mongodbDbName, mongodbUrl } = require('.. /.. /config/db');
// Generate an encapsulated mongodb instance to manipulate the database
function mongo() {
return new Mongodb(mongodbUrl, mongodbDbName);
}
module.exports = mongo;
Copy the code
config\db.js
// Database address
const mongodbUrl = 'mongo: / / 127.0.0.1:27017';
// Database name
const mongodbDbName = 'hzw-cli-dev';
module.exports = {
mongodbUrl,
mongodbDbName,
};
Copy the code
app\controller\project.js
const Controller = require('egg').Controller;
const mongo = require('.. /utils/mongodb') ();class ProjectController extends Controller {
async getTemplate() {
const { ctx } = this;
// Query the template collection
const data = await mongo.query('template'); ctx.body = data; }}module.exports = ProjectController;
Copy the code
Ok, the results are in.