亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

mysql - node.js怎么連接數(shù)據(jù)庫(kù)

瀏覽:77日期:2022-06-17 16:12:45

問(wèn)題描述

最近在學(xué)node.js,但是不知道怎么連接數(shù)據(jù)庫(kù)。沒(méi)有文檔可以看,求推薦。怎么用node連接mysql mongoDB redis solite?求解答

問(wèn)題解答

回答1:

var mysql = require(’mysql’);var connection = mysql.createConnection({ host : ’localhost’, user : ’me’, password : ’secret’, database : ’my_db’});connection.connect();connection.query(’SELECT 1 + 1 AS solution’, function(err, rows, fields) { if (err) throw err; console.log(’The solution is: ’, rows[0].solution);});connection.end();

https://github.com/mysqljs/my...

回答2:

var mongoose = require( ’mongoose’ )// Build the connection stringvar dbURI = ’mongodb://127.0.0.1/my_db’// Create the database connectionmongoose.connect(dbURI);mongoose.connection.on(’connected’, function () { console.log(’Mongoose connected to ’ + dbURI);})mongoose.connection.on(’error’,function (err) { console.log(’Mongoose connection error: ’ + err)})mongoose.connection.on(’disconnected’, function () { console.log(’Mongoose disconnected’)})process.on(’SIGINT’, function() { mongoose.connection.close(function () {console.log(’Mongoose disconnected through app termination’)process.exit(0) })})

以上是通過(guò)mongoose連接mongodb的代碼,保存為(db.js),在主程序(假設(shè)是app.js)中引入

var express = require(’express’);var db = require(’./model/db’) var app = express();//引入一個(gè)單獨(dú)的routerapp.use(’/account’, require(’./routes/account’))

即可在其他地方(假設(shè)是上面引入的一個(gè)單獨(dú)的router account.js)使用mongoose

var express = require(’express’)var mongoose = require(’mongoose’)require(’../model/user’)let UserModel = mongoose.model('User')

數(shù)據(jù)的model需要單獨(dú)建立,比如我們建立一個(gè)model/user.js文件,以供上面的router引入使用

var mongoose = require(’mongoose’)var Schema = mongoose.Schemavar userSchema = new Schema({ email:{type:String, index:true, unique:true}, unm:{type:String,index:true, unique:true}, password:String, role:{type:String, enum: ['student','controller','gm'], index:true}, lang:[String], regTime:Number, year:String, month:String, day:String,})mongoose.model(’User’,userSchema)

-----分割線--------------------------------------

redis可以用于session緩存,(當(dāng)然也可以用于暫存數(shù)據(jù)),用做緩存的話類似以下寫法,當(dāng)然我這里為了省事用的是mongodb臨時(shí)充當(dāng)緩存數(shù)據(jù)庫(kù)

var MongoStore = require(’connect-mongo’)(session)app.use(session({ secret: [’secret1’, ’secret2’], saveUninitialized: false, // don’t create session until something stored resave: false, //don’t save session if unmodified store: new MongoStore({url: ’mongodb://localhost/my_db’,touchAfter: 24 * 3600, // time period in secondsttl: 7 * 24 * 60 * 60 //session過(guò)期時(shí)間為7天,過(guò)期后使用cookies登陸 })}))回答3:

mysql連接可以使用庫(kù) mysqljs/mysql npm install mysqljs/mysqlmongo連接可以使用庫(kù) mongoose npm install mongooseredis連接可以使用庫(kù) redis npm install redissqlite連接可以使用庫(kù) sqlite3 npm install sqlite3

各個(gè)庫(kù)接口各不相同,請(qǐng)參考官方文檔mysql readmemongoose guideredis readmesqlite3 readme

回答4:

針對(duì)你這種的要不來(lái)幾個(gè)ORM試試??

Sequelize

Waterline

BOOKSHELF.JS

node-orm2

。。。。

根據(jù)你的需求,你需要連接的數(shù)據(jù)庫(kù)類型,是不是需要事務(wù)支持之類的,選擇合適的吧! 使用orm對(duì)你底層數(shù)據(jù)庫(kù)類型的切換會(huì)友好點(diǎn)。

相關(guān)文章:
主站蜘蛛池模板: 日韩视频一区 | 亚洲日本在线观看视频 | 国产福利视频在线播放 | 欧美区日韩区 | 精品国产系列 | 日韩精品一级毛片 | 欧美18性欧美丶黑吊 | 亚洲成a人片在线观看中 | 久久香蕉国产观看猫咪3atv | 一区二区三区无码高清视频 | 欧美最刺激好看的一级毛片 | 欧美日产欧美日产精品 | 精品国产1区 | 中文字幕成人在线 | 成人无遮挡免费网站视频在线观看 | 欧美色视频日本片高清在线观看 | 黄色小视频在线看 | 都市激情亚洲色图 | 成人性爱视频在线观看 | 成人男女网18免费看 | 亚洲欧美在线观看首页 | 国产乱子精品免费视观看片 | 91亚洲精品成人一区 | 成年人黄色大片 | 国产精品视频自拍 | 美女zw喷水视频在线观看 | 色婷婷综合久久久久中文 | 毛片一级片| 美国三级在线 | 婷婷国产天堂久久综合五月 | 韩国特黄毛片一级毛片免费 | 91精品国产综合成人 | 精品国内视频 | 欧美在线观看视频一区 | 小明看看永久视频 | 91福利国产在线观看网站 | 草草在线视频 | 国产三级在线观看免费 | 国产1区2区三区不卡 | 欧美亚洲国产激情一区二区 | 国产激烈无遮挡免费床戏视频 |