Getting started with express+mongodb under react

Express:

express is used to develop web interfaces

Install: npm install express --save

    //Directory: server/server.js
   
    const express = require('express');
    // New app
    const app = express(); 
        
    app.get('/', (req, res)=>{
        res.send('<h1>Hello world!</h1>')
    })
    
    app.get('/', (req. res)=>{
        res.json({desc:'Mareva who wants to lie down and win', job:'Monkey monkey'})
    })
        
    app.listen(9093,function(req, res){
        console.log('Node app start at port 9093')
    })
        

Enter the server / directory to execute: / > node server.js
Operation result:

localhost:9093/

localhost:9093/data

!!! If you modify the code, Cannot GET /data will appear on the page, execute the command again / > node server.js, MMP is troublesome, look down:

We can use nodemon to automatically restart express. Installation: npm install -g nodemon
Execute nodemon server.js, OK
After installation, there is no need for manual operation in the future~

Detailed use of express: poke me to jump

Mongodb+mongoose:

>>>Mongodb Download<<<

What is MongoDB?

MongoDB is written in C + +, which is an open source database system based on distributed file storage.

In the case of high load, adding more nodes can guarantee the server performance.

MongoDB aims to provide scalable high-performance data storage solutions for WEB applications.

MongoDB stores the data as a document, and the data structure consists of key value pairs. MongoDB documents are similar to JSON
Object. Field values can contain other documents, arrays and document arrays

Use the mongoose module of nodejs to link and operate mongodb, install / > NPM install mongoose -- save
>>>Mongoose detailed tutorial<<<

mongoose connecting to database


    //Directory: server/server.js
   
    const express = require('express');
  + const mongoose = require('mongoose');
  + const BD_URL = 'mongodb://localhost:27017'
  + // Linked database
  + mongoose.connect(DB_URL)
  + mongoose.connection.on('connected', function(){console.log('mongo Link success')})

    // New app
    const app = express(); 
        
    app.get('/', (req, res)=>{
        res.send('<h1>Hello world!</h1>')
    }) 
    
    app.get('/data', (req. res)=>{
        res.json({desc:'Mareva who wants to lie down and win', job:'Monkey monkey'})
    })
        
    app.listen(9093,function(req, res){
        console.log('Node app start at port 9093')
    })

Execute nodemon server.js

Define document model, Schema and modal new model


    //Directory: server/server.js
   
    const express = require('express');
    const mongoose = require('mongoose');
    const BD_URL = 'mongodb://localhost:27017'
    // Linked database
    mongoose.connect(DB_URL)
    mongoose.connection.on('connected', function(){console.log('mongo Link success')})
    
  + // Create User model
  + const User = mongoose.model('users', new mongoose.Schema({
  +     name:{type:String, require:true},
  +     age:{type:Number, require: true}
  + }))

  + // New data
  + User.create({
  +     name:'Kenny',
  +     age:30
  + },function(err, doc){
  +     if(!err)  {console.log(doc)}
  +     else{console.log(err)}
  + })


  + // Delete data
  + // User.remove({age:18}, function(err, doc){
  + //     console.log(doc)
  + // })

  + // Update data
  + // User.update({'name':'Kenny'}, {'$set':{age:30}}, function(err, doc){
  + //     console.log(doc)
  + // })

    // New app
    const app = express(); 
        
    app.get('/', (req, res)=>{
        res.send('<h1>Hello world!</h1>')
    }) 
    
    app.get('/data', (req. res)=>{
  - //    res.json({desc: 'marihua who wants to lie down and win', job: 'program ape'})

  +     // find, findOne
  +     User.find({user:'Kenny'}, function(err, doc){
  +         res.json(doc)
  +     })
    })
        
    app.listen(9093,function(req, res){
        console.log('Node app start at port 9093')
    })

Tags: node.js Mongoose MongoDB JSON Database

Posted on Sun, 08 Dec 2019 22:23:17 -0500 by wgh