koa,express,node閫氱敤鏂规硶杩炴帴MongoDB

Posted Node鍓嶇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了koa,express,node閫氱敤鏂规硶杩炴帴MongoDB相关的知识,希望对你有一定的参考价值。

鍏充簬杩炴帴mysql锛岀湅杩欑瘒鏂囩珷

杩欎釜鏂规硶锛宯ode锛宔xpress锛宬oa杩炴帴MongoDB閮芥槸涓€鏍风殑鏂规硶锛屾垜浠埄鐢╩ongoose妯″潡鏉ヨ繛鎺?/p>

杩欓噷鍙槸绠€鍗曞垪涓€涓嬭繛鎺ongoDB鐨勭粨鏋勶紝鍏跺疄瑕佺湡姝f噦MongoDB锛岃繕闇€瑕佸緢澶氱煡璇嗙偣锛屾瘮濡侻ongoDB鐨勬暟鎹被鍨嬶紝杩樻湁涓€浜汚PI鎿嶄綔

绠€鍗曡涓€涓婱ongoDB鐨勭粨鏋?/p>

鍏朵粬鍐呭涓嬫锛屽垎浜紝浠婂ぉ鍏堣繛涓婐煒?/p>


娉ㄦ剰鎴戠殑鐗堟湰

杩欐槸鎴戠殑鐩綍koa,express,node閫氱敤鏂规硶杩炴帴MongoDB

鎴戠殑瀹夎鐨勪緷璧栫増鏈細

 
   
   
 
  1. "dependencies": {

  2.    "koa": "^2.6.2",

  3.    "mongoose": "^5.3.12",

  4.  }`

1.杩炴帴MongoDB

 
   
   
 
  1. // db/db.js

  2. const mongoose = require('mongoose')

  3. const DB_URL = 'mongodb://localhost:27017/local'

  4. mongoose.connect(DB_URL)

  5. mongoose.connection.on('connected',function() {

  6.   console.log('Mongoose connection open to '+DB_URL);

  7. });

  8. /**

  9. * 杩炴帴寮傚父 error 鏁版嵁搴撹繛鎺ラ敊璇?/span>

  10. */

  11. mongoose.connection.on('error',function(err) {

  12.  console.log('Mongoose connection error: '+ err);

  13. });

  14. /**

  15. * 杩炴帴鏂紑 disconnected 杩炴帴寮傚父鏂紑

  16. */

  17. mongoose.connection.on('disconnected',function() {

  18.  console.log('Mongoose connection disconnected');

  19. });

  20. module.exports = mongoose

`

2.璁剧疆闇€瑕佹搷浣滅殑闆嗗悎

 
   
   
 
  1. // db/index.js

  2. const mongoose = require('./db')

  3. const Schema = mongoose.Schema;

  4. const ceshiSchema = new Schema({

  5.  title: String,

  6.  body: String,

  7.  date: Date

  8. });

  9. const MyModel = mongoose.model('ceshi', ceshiSchema);

  10. class Mongodb {

  11.  constructor () {

  12.  }

  13. // 鏌ヨ

  14.  query () {

  15.     return new Promise((resolve, reject) => {

  16.       MyModel.find({}, (err, res) => {

  17.         if(err) {

  18.           reject(err)

  19.         }

  20.         resolve(res)

  21.       })

  22.     })

  23.  }

  24. // 淇濆瓨

  25.  save (obj) {

  26.     const m = new MyModel(obj)

  27.     return new Promise((resolve, reject)=> {

  28.       m.save((err, res) => {

  29.         if (err) {

  30.           reject(err)

  31.         }

  32.         resolve(res)

  33.         console.log(res)

  34.       })

  35.     })

  36.  }

  37. }

  38. module.exports = new Mongodb()

杩欓噷灏辩畝鍗曚妇渚嬩簡鏌ヨ鍜屼繚瀛樻柟娉曪紝杩樻槸鍏朵粬鏂规硶锛屽彲浠ュ叿浣撳幓鐪嬫枃妗?/p>

3.璁剧疆鏈嶅姟鍣?/h2>
 
   
   
 
  1. // index.js

  2. const Koa = require('koa')

  3. const config = require('./config/default')

  4. const ModelDb = require('./db')

  5. const app =  new Koa()

  6. app.use(async (ctx) => {

  7.     let data = await ModelDb.query()

  8.    ctx.body = data

  9. })

  10. app.listen(config.port)

  11. console.log(`listening on port ${config.port}`)

4.鍚姩鏈嶅姟鍣?/h2>

鎵撳紑娴忚鍣紝灏卞彲浠ョ湅鐨勬煡璇㈠嚭鏉ョ殑缁撴灉

娉ㄦ剰锛氬墠鎻愬湪ceshi缁撳悎瑕佹湁鏂囨。锛屼篃灏辨槸瑕佹湁鏁版嵁

--




--


以上是关于koa,express,node閫氱敤鏂规硶杩炴帴MongoDB的主要内容,如果未能解决你的问题,请参考以下文章

aiohttp浣跨敤鏂规硶

JQuery甯哥敤鏂规硶鎬荤粨

椤圭洰鍙兘浼氱敤鍒扮殑杈呭姪鏂规硶

Python鐨凘property浣跨敤鏂规硶璇﹁В

uiautomatorviewer杩炴帴鏈哄櫒鐐瑰嚮鎶ラ敊Unexpected error while obtaining UI hierarchy

鐢╪ginx鍋氫唬鐞嗘湇鍔″櫒涓婄綉