2023-04-03

使用Node.js開發(fā)App的步驟

  1. 創(chuàng)建項(xiàng)目目錄

    mkdir myapp
    cd myapp
    # 合并以上兩步驟
    mkdir myapp && cd myapp
    
  2. 初始化

    npm init --yes //目的:創(chuàng)建package.json文件,該文件記錄了項(xiàng)目項(xiàng)目信息及項(xiàng)目中所有使用的模塊。
    
  3. 創(chuàng)建項(xiàng)目結(jié)構(gòu)

    myapp
    - views 
    - public
    - routes
    - package.json
    - package-lock.json
    - app.js
    
  4. 安裝模塊

    npm install express
    
  5. 引入模塊

    const express = require('express')
    
  6. 寫需求

    .....
    

網(wǎng)站根目錄

  1. 根目錄 VS 用戶根目錄

         \           vs        ~
    
  2. 查看當(dāng)前位置: pwd

  3. 項(xiàng)目根目錄:訪問權(quán)限設(shè)置為公開的磷脯、任何人都可以訪問的声怔。


public目錄

定義

Public 目錄是Node.js中存放網(wǎng)站靜態(tài)文件的目錄。靜態(tài)文件包括:

  • 圖片
  • css文件
  • js文件
  • 字體文件
mkdir public && cd public
mkdir css js images

Public : 公共的添瓷、共同的、公開


express框架

  • 框架的核心是構(gòu)造函數(shù)express()
  • Express()** 構(gòu)造函數(shù)用于創(chuàng)建一個(gè)APP實(shí)例(服務(wù)器類型的app)**
  • express是基于Node.js平臺(tái)
  • Node.js平臺(tái)是運(yùn)行js文件的。

  • Node.js項(xiàng)目目錄
    • index.js app.js server.js
    • views
      • html視圖模版
        • ejs視圖引擎
    • public
      • 引入靜態(tài)資源
  • 普通項(xiàng)目目錄
    • index.html
    • css
      • style.css
    • js
      • Script.js

知識(shí)點(diǎn)1:express.static()

定義

語法

返回值

示例


知識(shí)點(diǎn)2:創(chuàng)建數(shù)據(jù)庫

方案1:創(chuàng)建本地mongodb數(shù)據(jù)庫

  1. 啟動(dòng)mongodb數(shù)據(jù)庫

    # window系統(tǒng)
    服務(wù) => 右鍵 => 啟動(dòng)
    # Mac OS
    brew services start mongodb/brew/mongodb-community 
    
  2. 連接數(shù)據(jù)庫(怎么和數(shù)據(jù)庫通信陷寝?)

    1. 可以使用可視化軟件Compass
    2. 非可視化軟件:mongosh
mongosh "mongodb://localhost:27017"
  1. 創(chuàng)建數(shù)據(jù)庫: zhangsanblog

    1. 使用compass手動(dòng)創(chuàng)建

    2. 使用mongosh手動(dòng)創(chuàng)建

      use zhangsanblog
      
  2. 創(chuàng)建數(shù)據(jù)庫用戶

    1. 語法

      Db.createUser({
        user: 'zhangsan',
        pwd: '123456',
        roles: [{ role:"readWrite",db:"config"},"clusterAdmin"],
        roles: ["readWrite"]
      })
      

方案2:創(chuàng)建云數(shù)據(jù)庫

  1. 登錄Atlas賬戶
  2. 創(chuàng)建數(shù)據(jù)庫 : zhangsanblog
  3. 創(chuàng)建用戶: zhangsan Zxcvbn123456

知識(shí)點(diǎn)3:連接字符串

定義

連接字符串特指在App開發(fā)過程中連接數(shù)據(jù)庫的地址。

語法

"協(xié)議://用戶名:密碼@數(shù)據(jù)庫地址/數(shù)據(jù)庫名"

云數(shù)據(jù)庫的連接字符串

從云數(shù)據(jù)庫復(fù)制的連接字符串:
'mongodb+srv://<username>:<password>@zhangsanblog.4t6hj0s.mongodb.net/?retryWrites=true&w=majority'
用你的用戶名和密碼替換<username>和<password>
'mongodb+srv://zhaolusiblong:202303013@zhaolusiblong.towdlc9.mongodb.net/?retryWrites=true&w=majority'

本地?cái)?shù)據(jù)庫的連接字符串

"mongodb://zhangsan:123456@127.0.0.1:27107/zhangsanblog"
"mongodb://zhangsan:123456@localhost:27107/zhangsanblog"

知識(shí)點(diǎn)4:Node和MongoDB的通信

通信方式有兩種:

  • Mongodb模塊:
    • mongodb模塊是Node.js原生提供的與mongoDB數(shù)據(jù)庫通信的API其馏。
  • Mongoose模塊:
    • 是第三方提供的在Node平臺(tái)與MongoDB數(shù)據(jù)庫通信的方式凤跑。
    • Mongoose是一個(gè)庫。
    • 庫:就是函數(shù)的集合尝偎。
    • Mongoose庫包裹Node API饶火。

知識(shí)點(diǎn)5:使用Mongoose模塊連接數(shù)據(jù)庫

const mongoose = require('mongoose')

mongoose.connect(uri)
    .then((result) => {
        console.log('數(shù)據(jù)庫已經(jīng)連接')
    })
    .catch( err => console.log(err))

mongoose是什么?

  • 核心:mongoose()構(gòu)造函數(shù)
  • mongoose是一個(gè)ODM(對(duì)象數(shù)據(jù)模型)致扯。(object Data Model)
  • 使用js對(duì)象的語法來映射mongoose數(shù)據(jù)庫種的表和document
  • collection:數(shù)據(jù)庫中的表肤寝;
  • document:數(shù)據(jù)中的文檔;

打印mongoose

const mongoose = require('mongoose') 引入mongoose模塊會(huì)自動(dòng)創(chuàng)建mongoose實(shí)例對(duì)象抖僵。

  • mongoose是一個(gè)對(duì)象
  • Mongoose.connections 屬性
  • mongoose.Schema :構(gòu)造函數(shù)
  • Mongoose.model: 函數(shù)
Mongoose {
  connections: [
    NativeConnection {
      base: [Circular *1],
      collections: {},
      models: {},
      config: {},
      replica: false,
      options: null,
      otherDbs: [],
      relatedDbs: {},
      states: [Object: null prototype],
      _readyState: 0,
      _closeCalled: false,
      _hasOpened: false,
      plugins: [],
      id: 0,
      _queue: [],
      _listening: false
    }
  ],
  nextConnectionId: 1,
  models: {},
  events: EventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    [Symbol(kCapture)]: false
  },
  __driver: {
    Collection: [Function: NativeCollection],
    Connection: [Function: NativeConnection] { STATES: [Object: null prototype] }
  },
  options: {
    pluralization: true,
    autoIndex: true,
    autoCreate: true,
    [Symbol(mongoose:default)]: true
  },
  _pluralize: [Function: pluralize],
  Schema: [Function: Schema] {
    reserved: [Object: null prototype] {
      validate: 1,
      toObject: 1,
      save: 1,
      remove: 1,
      populated: 1,
      isNew: 1,
      isModified: 1,
      init: 1,
      get: 1,
      errors: 1,
      collection: 1,
      removeListener: 1,
      listeners: 1,
      emit: 1,
      prototype: 1
    },
    Types: {
      String: [Function],
      Number: [Function],
      Boolean: [Function],
      DocumentArray: [Function],
      Subdocument: [Function],
      Array: [Function],
      Buffer: [Function],
      Date: [Function],
      ObjectId: [Function],
      Mixed: [Function],
      Decimal: [Function],
      Decimal128: [Function],
      Map: [Function],
      UUID: [Function],
      Oid: [Function],
      Object: [Function],
      Bool: [Function],
      ObjectID: [Function]
    },
    ObjectId: [Function: ObjectId] {
      schemaName: 'ObjectId',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      _cast: [Function: castObjectId],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    }
  },
  model: [Function (anonymous)],
  plugins: [
    [ [Function: removeSubdocs], [Object] ],
    [ [Function: saveSubdocs], [Object] ],
    [ [Function], [Object] ],
    [ [Function: trackTransaction], [Object] ],
    [ [Function: validateBeforeSave], [Object] ]
  ],
  default: [Circular *1],
  mongoose: [Circular *1],
  cast: [Function: cast],
  STATES: [Object: null prototype] {
    '0': 'disconnected',
    '1': 'connected',
    '2': 'connecting',
    '3': 'disconnecting',
    '99': 'uninitialized',
    disconnected: 0,
    connected: 1,
    connecting: 2,
    disconnecting: 3,
    uninitialized: 99
  },
  setDriver: [Function: setDriver],
  set: [Function (anonymous)],
  get: [Function (anonymous)],
  createConnection: [Function (anonymous)],
  connect: [AsyncFunction: connect],
  disconnect: [AsyncFunction: disconnect],
  startSession: [Function (anonymous)],
  pluralize: [Function (anonymous)],
  deleteModel: [Function (anonymous)],
  modelNames: [Function (anonymous)],
  plugin: [Function (anonymous)],
  version: '7.0.3',
  Mongoose: [Function: Mongoose],
  SchemaType: [Function: SchemaType] {
    cast: [Function: cast],
    set: [Function: set],
    get: [Function (anonymous)],
    _isRef: [Function (anonymous)],
    checkRequired: [Function (anonymous)],
    CastError: [class CastError extends MongooseError],
    ValidatorError: [class ValidatorError extends MongooseError]
  },
  SchemaTypes: {
    String: [Function: SchemaString] {
      schemaName: 'String',
      defaultOptions: {},
      _cast: [Function: castString],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      get: [Function (anonymous)],
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Number: [Function: SchemaNumber] {
      get: [Function (anonymous)],
      set: [Function: set],
      _cast: [Function: castNumber],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      schemaName: 'Number',
      defaultOptions: {},
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Boolean: [Function: SchemaBoolean] {
      schemaName: 'Boolean',
      defaultOptions: {},
      _cast: [Function],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)],
      '$conditionalHandlers': [Object]
    },
    DocumentArray: [Function: DocumentArrayPath] {
      schemaName: 'DocumentArray',
      options: [Object],
      defaultOptions: {},
      set: [Function: set]
    },
    Subdocument: [Function: SubdocumentPath] {
      defaultOptions: {},
      set: [Function: set]
    },
    Array: [Function: SchemaArray] {
      schemaName: 'Array',
      options: [Object],
      defaultOptions: {},
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Buffer: [Function: SchemaBuffer] {
      schemaName: 'Buffer',
      defaultOptions: {},
      _checkRequired: [Function (anonymous)],
      set: [Function: set],
      checkRequired: [Function (anonymous)]
    },
    Date: [Function: SchemaDate] {
      schemaName: 'Date',
      defaultOptions: {},
      _cast: [Function: castDate],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    ObjectId: [Function: ObjectId] {
      schemaName: 'ObjectId',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      _cast: [Function: castObjectId],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Mixed: [Function: Mixed] {
      schemaName: 'Mixed',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set]
    },
    Decimal: [Function: Decimal128] {
      schemaName: 'Decimal128',
      defaultOptions: {},
      _cast: [Function: castDecimal128],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Decimal128: [Function: Decimal128] {
      schemaName: 'Decimal128',
      defaultOptions: {},
      _cast: [Function: castDecimal128],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Map: [class Map extends SchemaType] {
      schemaName: 'Map',
      defaultOptions: {}
    },
    UUID: [Function: SchemaUUID] {
      schemaName: 'UUID',
      defaultOptions: {},
      _cast: [Function (anonymous)],
      set: [Function: set],
      cast: [Function: cast],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Oid: [Function: ObjectId] {
      schemaName: 'ObjectId',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      _cast: [Function: castObjectId],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Object: [Function: Mixed] {
      schemaName: 'Mixed',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set]
    },
    Bool: [Function: SchemaBoolean] {
      schemaName: 'Boolean',
      defaultOptions: {},
      _cast: [Function],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)],
      '$conditionalHandlers': [Object]
    },
    ObjectID: [Function: ObjectId] {
      schemaName: 'ObjectId',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      _cast: [Function: castObjectId],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    }
  },
  VirtualType: [Function: VirtualType],
  Types: {
    Array: [Function: MongooseArray],
    Buffer: [Function: MongooseBuffer] {
      pathSymbol: Symbol(mongoose#Buffer#_path),
      mixin: [Object],
      Binary: [Function]
    },
    Embedded: [Function: ArraySubdocument] {
      _events: undefined,
      _eventsCount: 0,
      _maxListeners: undefined,
      setMaxListeners: [Function: setMaxListeners],
      getMaxListeners: [Function: getMaxListeners],
      emit: [Function: emit],
      addListener: [Function: addListener],
      on: [Function: addListener],
      prependListener: [Function: prependListener],
      once: [Function: once],
      prependOnceListener: [Function: prependOnceListener],
      removeListener: [Function: removeListener],
      off: [Function: removeListener],
      removeAllListeners: [Function: removeAllListeners],
      listeners: [Function: listeners],
      rawListeners: [Function: rawListeners],
      listenerCount: [Function: listenerCount],
      eventNames: [Function: eventNames]
    },
    Document: [Function: ArraySubdocument] {
      _events: undefined,
      _eventsCount: 0,
      _maxListeners: undefined,
      setMaxListeners: [Function: setMaxListeners],
      getMaxListeners: [Function: getMaxListeners],
      emit: [Function: emit],
      addListener: [Function: addListener],
      on: [Function: addListener],
      prependListener: [Function: prependListener],
      once: [Function: once],
      prependOnceListener: [Function: prependOnceListener],
      removeListener: [Function: removeListener],
      off: [Function: removeListener],
      removeAllListeners: [Function: removeAllListeners],
      listeners: [Function: listeners],
      rawListeners: [Function: rawListeners],
      listenerCount: [Function: listenerCount],
      eventNames: [Function: eventNames]
    },
    DocumentArray: [Function: MongooseDocumentArray],
    Decimal128: [class Decimal128 extends BSONValue],
    ObjectId: [class ObjectId extends BSONValue] { index: 7107676 },
    Map: [class MongooseMap extends Map],
    Subdocument: [Function: Subdocument]
  },
  Query: [Function: Query] {
    base: {
      toConstructor: [Function: toConstructor],
      setOptions: [Function (anonymous)],
      collection: [Function: collection],
      collation: [Function (anonymous)],
      '$where': [Function (anonymous)],
      where: [Function (anonymous)],
      equals: [Function: equals],
      eq: [Function: eq],
      or: [Function: or],
      nor: [Function: nor],
      and: [Function: and],
      gt: [Function (anonymous)],
      gte: [Function (anonymous)],
      lt: [Function (anonymous)],
      lte: [Function (anonymous)],
      ne: [Function (anonymous)],
      in: [Function (anonymous)],
      nin: [Function (anonymous)],
      all: [Function (anonymous)],
      regex: [Function (anonymous)],
      size: [Function (anonymous)],
      maxDistance: [Function (anonymous)],
      minDistance: [Function (anonymous)],
      mod: [Function (anonymous)],
      exists: [Function (anonymous)],
      elemMatch: [Function (anonymous)],
      within: [Function: within],
      box: [Function (anonymous)],
      polygon: [Function (anonymous)],
      circle: [Function (anonymous)],
      near: [Function: near],
      intersects: [Function: intersects],
      geometry: [Function: geometry],
      select: [Function: select],
      slice: [Function (anonymous)],
      sort: [Function (anonymous)],
      limit: [Function (anonymous)],
      skip: [Function (anonymous)],
      batchSize: [Function (anonymous)],
      comment: [Function (anonymous)],
      maxTimeMS: [Function (anonymous)],
      maxTime: [Function (anonymous)],
      hint: [Function (anonymous)],
      j: [Function: j],
      slaveOk: [Function (anonymous)],
      setReadPreference: [Function (anonymous)],
      read: [Function (anonymous)],
      r: [Function (anonymous)],
      readConcern: [Function (anonymous)],
      tailable: [Function (anonymous)],
      w: [Function: writeConcern],
      writeConcern: [Function: writeConcern],
      wTimeout: [Function: wtimeout],
      wtimeout: [Function: wtimeout],
      merge: [Function (anonymous)],
      find: [Function (anonymous)],
      _find: [AsyncFunction: _find],
      cursor: [Function (anonymous)],
      findOne: [Function (anonymous)],
      _findOne: [AsyncFunction: _findOne],
      count: [Function (anonymous)],
      _count: [AsyncFunction: _count],
      distinct: [Function (anonymous)],
      _distinct: [AsyncFunction: _distinct],
      updateMany: [Function: updateMany],
      _updateMany: [AsyncFunction (anonymous)],
      updateOne: [Function: updateOne],
      _updateOne: [AsyncFunction (anonymous)],
      replaceOne: [Function: replaceOne],
      _replaceOne: [AsyncFunction (anonymous)],
      deleteOne: [Function (anonymous)],
      _deleteOne: [AsyncFunction (anonymous)],
      deleteMany: [Function (anonymous)],
      _deleteMany: [AsyncFunction (anonymous)],
      findOneAndUpdate: [Function (anonymous)],
      _findOneAndUpdate: [AsyncFunction (anonymous)],
      findOneAndDelete: [Function (anonymous)],
      findOneAndRemove: [Function (anonymous)],
      _findOneAndRemove: [AsyncFunction (anonymous)],
      setTraceFunction: [Function (anonymous)],
      exec: [AsyncFunction: exec],
      then: [AsyncFunction (anonymous)],
      selected: [Function: selected],
      selectedInclusively: [Function: selectedInclusively],
      selectedExclusively: [Function: selectedExclusively],
      _mergeUpdate: [Function (anonymous)],
      _optionsForExec: [Function (anonymous)],
      _fieldsForExec: [Function (anonymous)],
      _updateForExec: [Function (anonymous)],
      _ensurePath: [Function (anonymous)],
      _validate: [Function (anonymous)]
    },
    'use$geoWithin': true
  },
  Model: Model { undefined },
  Document: [Function: Document] {
    _events: undefined,
    _eventsCount: 0,
    _maxListeners: undefined,
    setMaxListeners: [Function: setMaxListeners],
    getMaxListeners: [Function: getMaxListeners],
    emit: [Function: emit],
    addListener: [Function: addListener],
    on: [Function: addListener],
    prependListener: [Function: prependListener],
    once: [Function: once],
    prependOnceListener: [Function: prependOnceListener],
    removeListener: [Function: removeListener],
    off: [Function: removeListener],
    removeAllListeners: [Function: removeAllListeners],
    listeners: [Function: listeners],
    rawListeners: [Function: rawListeners],
    listenerCount: [Function: listenerCount],
    eventNames: [Function: eventNames],
    ValidationError: [class ValidationError extends MongooseError]
  },
  ObjectId: [Function: ObjectId] {
    schemaName: 'ObjectId',
    defaultOptions: {},
    get: [Function (anonymous)],
    set: [Function: set],
    _checkRequired: [Function (anonymous)],
    _cast: [Function: castObjectId],
    cast: [Function: cast],
    _defaultCaster: [Function (anonymous)],
    checkRequired: [Function (anonymous)]
  },
  isValidObjectId: [Function (anonymous)],
  isObjectIdOrHexString: [Function (anonymous)],
  syncIndexes: [Function (anonymous)],
  Decimal128: [Function: Decimal128] {
    schemaName: 'Decimal128',
    defaultOptions: {},
    _cast: [Function: castDecimal128],
    set: [Function: set],
    cast: [Function: cast],
    _defaultCaster: [Function (anonymous)],
    _checkRequired: [Function (anonymous)],
    checkRequired: [Function (anonymous)]
  },
  Mixed: [Function: Mixed] {
    schemaName: 'Mixed',
    defaultOptions: {},
    get: [Function (anonymous)],
    set: [Function: set]
  },
  Date: [Function: SchemaDate] {
    schemaName: 'Date',
    defaultOptions: {},
    _cast: [Function: castDate],
    set: [Function: set],
    cast: [Function: cast],
    _defaultCaster: [Function (anonymous)],
    _checkRequired: [Function (anonymous)],
    checkRequired: [Function (anonymous)]
  },
  Number: [Function: SchemaNumber] {
    get: [Function (anonymous)],
    set: [Function: set],
    _cast: [Function: castNumber],
    cast: [Function: cast],
    _defaultCaster: [Function (anonymous)],
    schemaName: 'Number',
    defaultOptions: {},
    _checkRequired: [Function (anonymous)],
    checkRequired: [Function (anonymous)]
  },
  Error: [class MongooseError extends Error] {
    messages: {
      DocumentNotFoundError: null,
      general: [Object],
      Number: [Object],
      Date: [Object],
      String: [Object]
    },
    Messages: {
      DocumentNotFoundError: null,
      general: [Object],
      Number: [Object],
      Date: [Object],
      String: [Object]
    },
    DocumentNotFoundError: [class DocumentNotFoundError extends MongooseError],
    CastError: [class CastError extends MongooseError],
    ValidationError: [class ValidationError extends MongooseError],
    ValidatorError: [class ValidatorError extends MongooseError],
    VersionError: [class VersionError extends MongooseError],
    ParallelSaveError: [class ParallelSaveError extends MongooseError],
    OverwriteModelError: [class OverwriteModelError extends MongooseError],
    MissingSchemaError: [class MissingSchemaError extends MongooseError],
    MongooseServerSelectionError: [class MongooseServerSelectionError extends MongooseError],
    DivergentArrayError: [class DivergentArrayError extends MongooseError],
    StrictModeError: [class StrictModeError extends MongooseError],
    StrictPopulateError: [class StrictPopulateError extends MongooseError]
  },
  now: [Function: now],
  CastError: [class CastError extends MongooseError],
  SchemaTypeOptions: [class SchemaTypeOptions],
  mongo: {
    BSON: [Getter],
    Binary: [Getter],
    BSONRegExp: [Getter],
    BSONSymbol: [Getter],
    BSONType: [Getter],
    Code: [Getter],
    DBRef: [Getter],
    Decimal128: [Getter],
    Double: [Getter],
    Int32: [Getter],
    Long: [Getter],
    MaxKey: [Getter],
    MinKey: [Getter],
    ObjectId: [Getter],
    Timestamp: [Getter],
    MongoBulkWriteError: [Getter],
    ChangeStreamCursor: [Getter],
    MongoAPIError: [Getter],
    MongoAWSError: [Getter],
    MongoBatchReExecutionError: [Getter],
    MongoChangeStreamError: [Getter],
    MongoCompatibilityError: [Getter],
    MongoCursorExhaustedError: [Getter],
    MongoCursorInUseError: [Getter],
    MongoDecompressionError: [Getter],
    MongoDriverError: [Getter],
    MongoError: [Getter],
    MongoExpiredSessionError: [Getter],
    MongoGridFSChunkError: [Getter],
    MongoGridFSStreamError: [Getter],
    MongoInvalidArgumentError: [Getter],
    MongoKerberosError: [Getter],
    MongoMissingCredentialsError: [Getter],
    MongoMissingDependencyError: [Getter],
    MongoNetworkError: [Getter],
    MongoNetworkTimeoutError: [Getter],
    MongoNotConnectedError: [Getter],
    MongoParseError: [Getter],
    MongoRuntimeError: [Getter],
    MongoServerClosedError: [Getter],
    MongoServerError: [Getter],
    MongoServerSelectionError: [Getter],
    MongoSystemError: [Getter],
    MongoTailableCursorError: [Getter],
    MongoTopologyClosedError: [Getter],
    MongoTransactionError: [Getter],
    MongoUnexpectedServerResponseError: [Getter],
    MongoWriteConcernError: [Getter],
    AbstractCursor: [Getter],
    Admin: [Getter],
    AggregationCursor: [Getter],
    CancellationToken: [Getter],
    ChangeStream: [Getter],
    ClientSession: [Getter],
    Collection: [Getter],
    Db: [Getter],
    FindCursor: [Getter],
    GridFSBucket: [Getter],
    GridFSBucketReadStream: [Getter],
    GridFSBucketWriteStream: [Getter],
    ListCollectionsCursor: [Getter],
    ListIndexesCursor: [Getter],
    MongoClient: [Getter],
    OrderedBulkOperation: [Getter],
    UnorderedBulkOperation: [Getter],
    BatchType: [Getter],
    GSSAPICanonicalizationValue: [Getter],
    AuthMechanism: [Getter],
    Compressor: [Getter],
    CURSOR_FLAGS: [Getter],
    AutoEncryptionLoggerLevel: [Getter],
    MongoErrorLabel: [Getter],
    ExplainVerbosity: [Getter],
    ServerApiVersion: [Getter],
    ReturnDocument: [Getter],
    ProfilingLevel: [Getter],
    ReadConcernLevel: [Getter],
    ReadPreferenceMode: [Getter],
    ServerType: [Getter],
    TopologyType: [Getter],
    ReadConcern: [Getter],
    ReadPreference: [Getter],
    WriteConcern: [Getter],
    CommandFailedEvent: [Getter],
    CommandStartedEvent: [Getter],
    CommandSucceededEvent: [Getter],
    ConnectionCheckedInEvent: [Getter],
    ConnectionCheckedOutEvent: [Getter],
    ConnectionCheckOutFailedEvent: [Getter],
    ConnectionCheckOutStartedEvent: [Getter],
    ConnectionClosedEvent: [Getter],
    ConnectionCreatedEvent: [Getter],
    ConnectionPoolClearedEvent: [Getter],
    ConnectionPoolClosedEvent: [Getter],
    ConnectionPoolCreatedEvent: [Getter],
    ConnectionPoolMonitoringEvent: [Getter],
    ConnectionPoolReadyEvent: [Getter],
    ConnectionReadyEvent: [Getter],
    ServerClosedEvent: [Getter],
    ServerDescriptionChangedEvent: [Getter],
    ServerHeartbeatFailedEvent: [Getter],
    ServerHeartbeatStartedEvent: [Getter],
    ServerHeartbeatSucceededEvent: [Getter],
    ServerOpeningEvent: [Getter],
    TopologyClosedEvent: [Getter],
    TopologyDescriptionChangedEvent: [Getter],
    TopologyOpeningEvent: [Getter],
    SrvPollingEvent: [Getter]
  },
  mquery: [Function: Query] {
    permissions: {
      distinct: [Function],
      findOneAndRemove: [Function],
      findOneAndUpdate: [Function],
      count: [Function]
    },
    _isPermitted: [Function (anonymous)],
    canMerge: [Function (anonymous)],
    setGlobalTraceFunction: [Function (anonymous)],
    utils: {
      clone: [Function: clone],
      cloneObject: [Function: cloneObject],
      cloneArray: [Function: cloneArray],
      merge: [Function: merge],
      mergeClone: [Function: mergeClone],
      readPref: [Function: readPref],
      readConcern: [Function: readConcern],
      toString: [Function (anonymous)],
      isObject: [Function (anonymous)],
      keys: [Function: keys],
      create: [Function: create],
      inherits: [Function (anonymous)],
      isArgumentsObject: [Function (anonymous)]
    },
    env: { isNode: [Array], isMongo: false, isBrowser: false, type: 'node' },
    Collection: [class NodeCollection extends Collection],
    BaseCollection: [Function: Collection] { methods: [Array] }
  },
  sanitizeFilter: [Function: sanitizeFilter],
  trusted: [Function: trusted],
  skipMiddlewareFunction: [Function: skipWrappedFunction],
  overwriteMiddlewareResult: [Function: overwriteResult]
}

mongoose是什么鲤看?

Mongoose 是 MongoDB 的對(duì)象建模工具。

Mongoose 用于和MongoDB 通信(增刪改查)耍群。

Mongoose運(yùn)行在Node.js 平臺(tái)义桂。

  • Mongoose()是一個(gè)構(gòu)造函數(shù)
  • 引入mongoose模塊會(huì)自動(dòng)創(chuàng)建一個(gè)mongoose實(shí)例對(duì)象。

mongoose.connect()

  • 定義:

mongoose.connect()用于連接MongoDB數(shù)據(jù)庫蹈垢。

它是一個(gè)異步函數(shù)慷吊。

語法

mongoose.connect(uri)
mongoose.connect(數(shù)據(jù)庫連接字符串)

返回值

返回一個(gè)Promise對(duì)象

示例


promise.then()

定義

then()方法為promise綁定回調(diào)函數(shù)。

語法

promise.then(resolved,rejected)
promise.then((data) => {
  //在異步操作成功時(shí)曹抬,處理異步操作的結(jié)果
},(error) => {
  //在異步操作失敗時(shí)溉瓶,輸出錯(cuò)誤的原因
})

//一個(gè)參數(shù):大多時(shí)候我們更加關(guān)注異步操作成功
promise.then((resolved) => {
  //在異步操作成功時(shí),處理異步操作的結(jié)果
})

//then()方法可以鏈?zhǔn)秸{(diào)用
promise.then((resolved) => {
  //在異步操作成功時(shí),處理異步操作的結(jié)果
})
  .then((resolved) => {})
    .then((resolved) => {})
    .catch(error => {//捕獲錯(cuò)誤信息})

then() 方法最多接受兩個(gè)參數(shù):

  • resolved :Promise 已完成的回調(diào)函數(shù)堰酿。

  • rejected: Promise已拒絕的回調(diào)函數(shù)疾宏。

返回值

  • 返回一個(gè)等效的prmose對(duì)象。

示例

promise.catch()

  • 定義:

catch()方法用于為promise對(duì)象綁定請(qǐng)求失敗時(shí)的回調(diào)函數(shù)触创。

catch()方法用于捕獲錯(cuò)誤信息坎藐。

語法

promise.catch((err) => {})

返回值

返回一個(gè)promise對(duì)象。

示例

mongoose.model()

  • 定義:

用于創(chuàng)建與MongoDB數(shù)據(jù)庫通信的工具哼绑。

該工具在這里意思就是:指一個(gè)構(gòu)造函數(shù)岩馍。

構(gòu)造函數(shù)可以實(shí)例化一個(gè)對(duì)象。

通過實(shí)例對(duì)象的方法與數(shù)據(jù)庫進(jìn)行通信凌那。

語法

const Blog = mongoose.model('模型名稱',模型原型)
const Blog = mongoose.model('模型名稱',blogSchema)

返回值

返回大寫的構(gòu)造函數(shù)(Model())

示例

Schema()

  • 定義:

Schema()是一個(gè)構(gòu)造函數(shù)/類兼雄。

Schema()用于描述數(shù)據(jù)表的結(jié)構(gòu)。

  • 數(shù)據(jù)庫:由多個(gè)數(shù)據(jù)庫表構(gòu)成
  • 數(shù)據(jù)庫表:由多個(gè)document(記錄)構(gòu)成帽蝶。
  • Document: 由字段和值構(gòu)成赦肋。

語法

 new Schema({定義表結(jié)構(gòu)},{選項(xiàng)對(duì)象})

const blogSchema = new Schema({
  //定義title字段
  title: {
    //字段類型:字符串
    type: String,
    //字段是否必寫: true
    required: true
  },
  body: {
    type: String,
    required: true
  }
}, {})
  • 定義表結(jié)構(gòu): 定義有哪些字段、每個(gè)字段的數(shù)據(jù)類型励稳、是否必填等
  • 選項(xiàng)對(duì)象:提供了一些參數(shù)佃乘,用來配置第一個(gè)參數(shù)

返回值

返回schema實(shí)例對(duì)象,表示數(shù)據(jù)結(jié)構(gòu)。

示例:


Bootstrap:.Containers

<div class='Containers'></div>
-定義:
- 表示一個(gè)容器驹尼。
- Containers類是Bootstrap是一個(gè)構(gòu)建基本結(jié)構(gòu)的類趣避;
- 用法1:寬度固定

<div class='Containers'></div>

- 用法2:寬度不固定100%;流式布局

<div class="container-fluid"></div>

------
Background:背景色+文字顏色

bg-body-tertiary


-------
#.link:設(shè)置鏈接顏色
- 定義:link-primary




?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末新翎,一起剝皮案震驚了整個(gè)濱河市程帕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌地啰,老刑警劉巖愁拭,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異亏吝,居然都是意外死亡岭埠,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門蔚鸥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來惜论,“玉大人,你說我怎么就攤上這事止喷」堇啵” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵弹谁,是天一觀的道長(zhǎng)蹦掐。 經(jīng)常有香客問我技羔,道長(zhǎng),這世上最難降的妖魔是什么卧抗? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮鳖粟,結(jié)果婚禮上社裆,老公的妹妹穿的比我還像新娘。我一直安慰自己向图,他們只是感情好泳秀,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著榄攀,像睡著了一般嗜傅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上檩赢,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天吕嘀,我揣著相機(jī)與錄音,去河邊找鬼贞瞒。 笑死偶房,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的军浆。 我是一名探鬼主播棕洋,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼乒融!你這毒婦竟也來了掰盘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤赞季,失蹤者是張志新(化名)和其女友劉穎愧捕,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體碟摆,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡晃财,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了典蜕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片断盛。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖愉舔,靈堂內(nèi)的尸體忽然破棺而出钢猛,到底是詐尸還是另有隱情,我是刑警寧澤轩缤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布命迈,位于F島的核電站贩绕,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏壶愤。R本人自食惡果不足惜淑倾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望征椒。 院中可真熱鬧娇哆,春花似錦、人聲如沸勃救。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蒙秒。三九已至勃黍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間晕讲,已是汗流浹背覆获。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留益兄,地道東北人锻梳。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像净捅,于是被迫代替她去往敵國和親疑枯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 使用Node.js開發(fā)APP的步驟 1.創(chuàng)建項(xiàng)目目錄 2.初始化 3.創(chuàng)建項(xiàng)目結(jié)構(gòu) 4.安裝模塊 5.引入模塊 6...
    貝兼全_c5e4閱讀 96評(píng)論 0 0
  • 使用Node.js開發(fā)App的步驟 創(chuàng)建項(xiàng)目目錄mkdir myappcd myapp# 合并以上兩步驟mkdir...
    天天涯閱讀 67評(píng)論 0 0
  • Node.js第一天 1. 初識(shí)Node.js 1.1 Node.js是什么 Node.js? is a Java...
    再見天才閱讀 4,738評(píng)論 1 24
  • 使用Node.js開發(fā)App的步驟 創(chuàng)建項(xiàng)目目錄mkdir myappcd myapp# 合并以上兩步驟mkdir...
    憂油魚閱讀 333評(píng)論 0 1
  • Node介紹 為什么要學(xué)習(xí)Node.js 企業(yè)需求具有服務(wù)端開發(fā)經(jīng)驗(yàn)更改front-endback-end全棧開發(fā)...
    ax_43d8閱讀 401評(píng)論 0 0