Express builds the project and connects to the database

(1) Before installing node.js [install nvm]:
There is an easy way to install nvm:
$ wget -qO- https://raw.github.com/creationix/nvm/v0.25.0/install.sh | sh
This installation actually clones nvm into ~/.nvm. The scource added in ~/.bashrc and ~/.zshrc are both set up during the installation.
If your nvm version is a lower version, you can also checkout the new version installation in ~/.nvm/, and it will be updated automatically.

(2) Next [Install node]:
After installing nvm, you can directly use nvm to install node. First, use nvm to view all existing node versions:
$ nvm ls-remote  
[nvm will install npm when installing node]
and then select Install the version you want: $ nvm install <version>
If multiple versions are installed, you can use: nvm use <version> to select the version you want.
nvm current to view the current node version, node ls can also view the local node
to set the default version through the command: $ nvm alias default <version>
After installation, you can view the version you have installed through node -v

(3) [Install express]:
 $ nvm install -g express-generator

At this time express is the latest version. You can check whether your version is 4x by: $ express version.
For other express commands, you can go to the express tutorial

 

(4) [Create a project through express:]

Enter the following command on the command line:

$ express -e blog  
$ cd blog && npm install

 At this point, your project named blog has been successfully built, and then run the following command:

$ DEBUG=blog:* npm start

 After visiting in the browser; localhost:3000, you will see the default interface of express.

Let's take a look at the project's [engineering structure]:
app.js: startup file, or entry file
package.json: stores project information and module dependencies. When adding dependent modules in dependencies, run npm install, npm It will check the package.json in the current directory and automatically install all the specified modules
node_modules: store the modules installed in package.json, when you add dependent modules in package.json and install them, store them in this folder
public: Store image, css, js and other files
routes: store routing files
views: store view files or template files
bin: store executable files

 

(5) [Connect to the database:] There are two methods, the author recommends using the first one.

Create project database and super administrator:
[Operate database] database collection collection, document setting insert
terminal command line:
mongo//Enter database
use nodedb//Create project database
db.addUser("shuaige", "123456")// Created an account named Handsome Guy for this database with a password of 123456 (but I think I may not understand it well, you can also not do this operation)

If the database is started in authorized mode, then we can perform administrator verification, and then we can continue Operation
Then, we create a collection (collection is equivalent to a table in oracle and mysql)
database code for this nodedb database

db.createCollection("users")//Create a collection, which is a table
db.users.insert({user: "admin", password: "123456"})//Add a document to users, that is, a record account admin, password 123456  

 ok, now check it out:
db.users.find() //if you see the documentation you just added, ok

 

Now I already have the user admin in my nodedb database, and then the operation is performed in the nodedb database

 

 

【Method Two:】

Open the package.json file, add in the last line of dependencies: "mongodb": "1.4.15"
and then run in the project directory: npm install to update the dependent modules.
After the mongodb module is installed, create a settings.js file in the project directory and add:

module.exports = {   
      cookieSecret: 'myblog',   
      db: 'blog',   
      host: 'localhost',  
      port: 27017  
    };

 Then create a new models folder, and create a new db.js file in the folder, add:

var settings = require('../settings'),  
        Db = require('mongodb').Db,  
        Connection = require('mongodb').Connection,  
        Server = require('mongodb').Server;  
    module.exports = new Db(settings.db, new Server(settings.host, settings.port),  
    {fe: true});

 Then open app.js, add under var routes = require('./routes/index');:
var settings = require('./settings'); import the file

 

(6) [Session support:]
To store session information in the database for lasting maintenance, we need the help of two middleware, express-session and connect-mongo. Add in the package.json file:
    "express-session": "1.9.1", 
    "connect-mongo": "0.4.1" 
After updating the module npm install, open the app.js file and add:

var session = require('express-session');
    var MongoStore = require('connect-mongo')(session);
     
    app.use(session({
      secret: settings.cookieSecret,
      key: settings.db,//cookie name
      cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//cookie expiration time 30 days
      store: new MongoStore({
        db: settings.db,
        host: settings.host,
        port: settings.port
      })
    }));  

 
 Note: Here is a reminder that the location of these two pieces of code is a problem. The above two variables are placed in the position of the variables in the file. The following paragraph should be placed in:

 

app.use(express.static(path.join(__dirname, 'public'))); 
above this code.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326562780&siteId=291194637