express数据库链接
时间:2021-07-01 10:21:17
帮助过:3人阅读
MongoClient = require(
‘mongodb‘).MongoClient;
const DB_CONN_STR =
‘mongodb://localhost:27017/test‘;
let insertData =
function(db, data, callback) {
let collection = db.collection(
‘person‘);
collection.insert(data, function(err, result) {
if (err) {
console.log(‘Error:‘ +
err);
return;
}
callback(result);
})
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
console.log(‘连接成功!‘);
let data = [{
"name":
"tom",
"age":
"99" }];
insertData(db, data, function(result) {
console.log(result);
db.close();
})
selectData(db, function(result) {
console.log(result);
db.close();
});
})
2. 使用mongoose中间件链接:
链接mysql的方式:
1.原始链接方式:
const mysql = require(‘mysql‘);
let connection = mysql.createConnection({
host: ‘localhost‘,
user: ‘root‘,
password: ‘368532‘,
database: ‘world‘
});
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();
2.mysql中pool方式: mysql.createPool({})
3.使用中间件express-myconnection链接:
express数据库链接
标签:style const llb localhost 使用 mysq require expr pool