Express学习之指南<3>

六.代理背后的Express

当在一个代理背后运行Express应用时,使用app.set()这个方法将应用变量trust proxy的值设置成下面表中列出的其中一个值。

如果应用变量trust proxy没有被设置的话,应用运行并不会失败。除非trust proxy被配置,否则的话,应用就会错误地将代理的IP地址注册成客户端的IP地址。

如果trust proxy的值不是false的话,就会导致两个重要的变化:

  • X-Forwarded-Proto可能会被相反的代理设置来告诉应用它是https还是http,这个值由req.protocol绝定。
  • req.ipreq.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);
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值