Reaction + Redux Router + node practice summary (express+mongodb)

Keywords: MongoDB Mongoose JSON Database

Express+mongodb Develops web Background Interface

  • Non-relational database mongodb (similar to mySql, which stores json data)
    Using the mongoose module of nodeJS to connect mongodb to operate mongodb data

Express

Rapid, open and minimal web development framework based on nodejs
Express is a framework for node to develop web interfaces for listening to interfaces, rendering pages, and similar PHP CI frameworks
1. Install express
npm install express –save
2. express application

// The node should use require to load files, import belongs to ES6 grammar
const express = require('express')

// New app
const app = express()
// App. get (path, function (request, response)
// '/'represents the root directory
app.get('/', function(req, res) {
    res.send('<h1>hello world</h1>')
})
// Execution time localhost:9093/data
// The page shows {name:'lmh', type:'IT')
app.get('/data', function(req, res) {
    res.json({name: 'lmh', type: 'IT'})
})
// Monitor 9093 Interface
app.listen(9093, function() {
    console.log('Node app start at port 9093')
})
// The runtime needs to switch to this file and use the node file name to start the file
// View the execution results in browser localhost:9093
  1. Listen for routing and response content and restart automatically using nodemon
    npm install -g nodemon // install nodemon
    It automatically monitors code changes, and when the code changes, it does not need to manually execute the node file name, and nodemon automatically restarts the service.
  2. Other features of expores
    1. app.get and app.post develop get and post interfaces respectively
    2. app.use uses modules (in complex applications, routing is split into multiple modules, and use is used to reference modules in the main module)
    3. Generation res. send (text content), res.json(json data), res. sendfile (file) responds to different content

Mongodb: Non-relational database

Official website https://www.mongodb.com/ Download and install mongodb
// Run mongodb
mongod –config /usr/local/etc/mongod.conf
mongo
// Install mongoose to connect to mongodb
npm install mongoose –save
1. Mongoose Foundation Use
- Connect to the database
- Define document models (similar to tables in mysql), and create new models for Schema and model
Generate a database document corresponding to a model, through which the database is operated
2. Mongoose document type
1. String, Number and other data structures
2. Define the operations of create, remove and update to add, delete and change, respectively.
3. Find and findOne are used to query data
3. Express+mongodb
crud
1. mongod-config/usr/local/etc/mongod.conf background boot
2. Express combines mongodb
3. Packaging mongoose
4. Follow-up: Express and mongodb
1. mongodb Independent Tool Function
2. express supports post parameters using body-parser
3. Use cookie-parser to store login information cookie

// Import (ES6) cannot be used to import files in node, so require is used to load files.
const express = require('express')
const mongoose = require('mongoose')

//Connect mongodb and use the imooc collection
const DB_URL = 'mongodb://localhost:27017/imooc'
mongoose.connect(DB_URL)
mongoose.connection.on('connected', function() {
    console.log('mongo connect success')
})
// Mongodb is similar to mysql tables. mongodb contains the concepts of documents and fields.
// Using mongoose to manipulate mongodb, json is stored, which is much easier to use than mysql.
// Mongoose. model (table name, data set)
const User = mongoose.model('user', new mongoose.Schema({
    // Type data structure type, require: Is the field mandatory
    user: {type:String, require: true},
    age: {type:Number, require: true}
}))
// New data: create (data, callback function after new data is successful)
User.create({
    user: 'imooc',
    age: 18
}, function(err, doc) {
    // The first parameter for most callback functions in node is err (error message)
    if (!err) {
        console.log(doc)
    } else {
        console.log(err)
    }
})
// New app
const app = express()
// App. get (path, function (request, response)
// '/'represents the root directory
app.get('/', function(req, res) {
    res.send('<h1>hello world</h1>')
})
// Execution time localhost:9093/data
// The page shows {name:'lmh', type:'IT')
app.get('/data', function(req, res) {
    // res.json({name: 'lmh', type: 'IT'})
    // find all data: find shows all the data found, findOne only finds one data
    User.find({}, function(err, doc) {
        res.json(doc)
    })
})
// Delete data
app.get('/delete', function(req, res) {
    User.remove({age: 18}, function(err, doc) {
        res.json(doc)
    })
})
// Update data
app.get('/update', function(req, res) {
    // Update (which record to update, {set': which values to update the current record} and the callback function after the update is successful)
    User.update({'user': 'imooc'}, {'$set': {age: 26}}, function(err, doc) {
        res.json(doc)
    })
})

// Monitor 9093 Interface
app.listen(9093, function() {
    console.log('Node app start at port 9093')
})

Posted by rulinus on Mon, 07 Jan 2019 08:39:10 -0800