Install MongoDB and the configuration file

1. Download and install

You can choose your favorite directory to install (such as disk D) select custorm Settings directory

2. Check whether MongoDB is successfully installed and run the startup command

In the bin directory of the MongoDB installation directory, open the CMD command line and run the mongod –help[mongod -h] command.

3. Set the storage locations for data files and log files

Set the storage location of files according to their own preferences. The storage location of this installation document is set in disk E, and the directory is as follows

    E:\MongoDATA

├─ Data #MongoDB for storing data in the database

├ ─log #MongoDB store logs in the database

├ ─ imp. Log # save logs to mongodb

4. Configure the MongoDB service

(1) Set environment variables of MongoDB

Computer > Right-click > Properties > Advanced System Settings > Environment Variables > Find the path variable in System Variables

Add the address of the bin directory in the MongoDB installation directory to the end of the path variable

             

(2) Install the MongoDB service

At the beginning, open the CMD command line as administrator, type the following modified command, and press Enter (ps: Be sure to run as administrator)

Modify the following command according to the set storage location (ps: if the storage location is set according to this document, do not need to change)

mongod --logpath "E:\MongoData\log\mongodb.log" --logappend --dbpath
"E:\MongoData\data" --directoryperdb --serviceName  "MongoDB" --serviceDisplayName "MongoDB" --install
Copy the code

Description of the fields in the command

******* –logpath Specifies the path where MongoDB log files are stored

******* — logAppend writes logs in the append mode

** *** ** –dbpath Specifies the path for storing database data

******* –directoryperdb A new directory is created for each DB

******* –serviceName specifies the serviceName

******* –serviceDisplayName Specifies the service name. Run this command if there are multiple mongodb services.

******* –install specifies to install as a Windows service.

5. Start the MongoDB service

In the bin directory of the MongoDB installation directory, open the CMD command line (the server and client have two CMD and cannot be closed).

(1) Start the client mongo

Mongod –dbpath:E:/MongoData/data

2. Build node service with Express

1. Create a directory, open CMD (terminal) in this directory, and initialize NPM init

A package.json file is automatically generated containing the project name, version number, project description, entry file, unit test, remote repository, keyword, author, license

2. Project directory app.js entry file change package.json main to “main”:”app.js” various modules are introduced

3. In the bin directory is the WWW configuration file to start the file.

"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node ./bin/www", "zs": /bin/ WWW/node. /bin/ WWW/node. /bin/ WWWCopy the code

Routers are routing files (including the following files)

Setting. js The path to the database link

Db. Js linked database encapsulates the method of adding, deleting, changing and searching data

Index.js is an interface file (express.router())

Views is a static resource file that can be without

4. Set up a simple server

Var HTTP =require(" HTTP "); 2. Create server: Varserver = http.createserver (function(req,res){res.end() // Response end}) where req is short for request. Listen (3000, "127.0.0.1") localhost *** Express was introduced primarily for routing (so called interfaces) containing a lot of middlewareCopy the code

5. Two ways of exposing modules and their differences

Exports exposes module module. Exports exposes an object distinction: Module. exports starts with an empty object {} that can only appear once for a class (constructor). Exports refers to modules Module: exports: exports. MSG = exports; // MSG is an exports.show=show; //show is a function of module.exports: module.exports={MSG: MSG, // MSG is a variable of show:show //show is a function of} module.exports=People; //People is a constructorCopy the code

The various modules downloaded into production are generated in package.json

Finally, NPM start starts