六.代理背后的Express
当在一个代理背后运行Express应用时,使用app.set()这个方法将应用变量trust proxy的值设置成下面表中列出的其中一个值。
如果应用变量trust proxy没有被设置的话,应用运行并不会失败。除非trust proxy被配置,否则的话,应用就会错误地将代理的IP地址注册成客户端的IP地址。
如果trust proxy的值不是false的话,就会导致两个重要的变化:
X-Forwarded-Proto可能会被相反的代理设置来告诉应用它是https还是http,这个值由req.protocol绝定。- req.ip和req.ips的值是在
X-Forwarded-For列出的地址当中。
trust proxy的设置是由proxy-addr这个包来实现的。想了解更多,请看它的文档。
七.数据库集成
为Express应用添加数据库连接功能只需要为你应用中的数据库加载一个合适的Node.js驱动。
这篇文档简单地解释了如何在你的Express应用中,为你的数据库系统添加和使用最流行的Node模块。
1.Cassandra
模块:cassandra-driver
安装:
$ npm install cassandra-driver
代码例子:
var cassandra = require('cassandra-driver');
var client = new cassandra.Client({ contactPoints: ['localhost']});
client.execute('select key from system.local', function(err, result) {
if (err) throw err;
console.log(result.rows[0]);
});
2.CouchDB
模块:nano
安装:
$ npm install nano
代码例子:
var nano = require('nano')('http://localhost:5984');
nano.db.create('books');
var books = nano.db.use('books');
//Insert a book document in the books database
books.insert({name: 'The Art of war'}, null, function(err, body) {
if (!err){
console.log(body);
}
});
//Get a list of all books
books.list(function(err, body){
console.log(body.rows);
}
3.LevelDB
模块: levelup
安装:
$ npm install level levelup leveldown
代码例子:
var levelup = require('levelup');
var db = levelup('./mydb');
db.put('name', 'LevelUP', function (err) {
if (err) return console.log('Ooops!', err);
db.get('name', function (err, value) {
if (err) return console.log('Ooops!', err);
console.log('name=' + value)
});
});
4.MySQL
模块:mysql
安装:
$ npm install mysql
代码例子:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'dbuser',
password : 's3kreee7'
});
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();
5.MongoDB
模块:mongoskin
安装:
$ npm install mongoskin
代码例子:
var db = require('mongoskin').db('mongodb://localhost:27017/animals');
db.collection('mamals').find().toArray(function(err, result) {
if (err) throw err;
console.log(result);
});
如果你需要MongoDB的一个对象模型驱动,请看Mongoose
6.Neo4j
模块:apoc
安装:
$ npm install apoc
代码例子:
var apoc = require('apoc');
apoc.query('match (n) return n').exec().then(
function (response) {
console.log(response);
},
function (fail) {
console.log(fail);
}
);
7.PostgreSQL
模块:pg
安装:
$ npm install pg
代码例子:
var pg = require('pg');
var conString = "postgres://username:password@localhost/database";
pg.connect(conString, function(err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
done();
if (err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
});
});
8.Redis
模块: redis
安装:
$ npm install redis
代码例子:
var client = require('redis').createClient();
client.on('error', function (err) {
console.log('Error ' + err);
});
client.set('string key', 'string val', redis.print);
client.hset('hash key', 'hashtest 1', 'some value', redis.print);
client.hset(['hash key', 'hashtest 2', 'some other value'], redis.print);
client.hkeys('hash key', function (err, replies) {
console.log(replies.length + ' replies:');
replies.forEach(function (reply, i) {
console.log(' ' + i + ': ' + reply);
});
client.quit();
});
9.SQLite
模块: sqlite3
安装:
$ npm install sqlite3
代码例子:
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run('CREATE TABLE lorem (info TEXT)');
var stmt = db.prepare('INSERT INTO lorem VALUES (?)');
for (var i = 0; i < 10; i++) {
stmt.run('Ipsum ' + i);
}
stmt.finalize();
db.each('SELECT rowid AS id, info FROM lorem', function(err, row) {
console.log(row.id + ': ' + row.info);
});
});
db.close();
10.ElasticSearch
模块:elasticsearch
安装:
$ npm install elasticsearch
代码例子:
var elasticsearch = require('elasticsearch');
var client = elasticsearch.Client({
host: 'localhost:9200'
});
client.search({
index: 'books',
type: 'book',
body: {
query: {
multi_match: {
query: 'express js',
fields: ['title', 'description']
}
}
}
}).then(function(response) {
var hits = response.hits.hits;
}, function(error) {
console.trace(error.message);
});
本文介绍了如何在Express应用中集成多种数据库,包括Cassandra、CouchDB、LevelDB、MySQL等,并提供了每种数据库的安装步骤及示例代码。
540

被折叠的 条评论
为什么被折叠?



