nodejs review-03

39 Serve different file types with our server

  • 处理文件类型
function content_type(filename) {
  var ext = path.extname(filename).toLowerCase();
  switch(ext) {
    case '.jpg': case '.jpeg':
      return 'image/jpeg';
    case '.gif':
      return 'image/gif';
    case '.png':
      return 'image/png';
    case '.css':
      return 'text/css';
    case '.js':
      return 'text/javascript';
    case '.html':
      return 'text/html';
    default:
      return 'text/plain';
  }
}
  • 返回时数据类型的判断
//如果不是直接使用pipe处理数据流的话

if(content_type(fn).substr(0, 6) === 'image/') {
  res.write(data);
} else {
  res.write(data.toString('utf8');
}

  • 使用curl下载
curl -o filename.type http://localhost:3000         //下载传输过来的文件

53 Support POST data, cookies, and sessions

  • curl传输表单数据
curl -X -POST -F(--form) email=example.gmail  -F 'username=jinks peng'  localhost:3000;
  • express cookie简单操作
//设置
res.cookie(name, value [, options])

//清除
res.clearCookie(name [, options])
  • express文件的简便操作
//下载文件
res.download(path [, filename] [, fn])

//发送文件
res.sendFile(path [, options] [, fn])

55 Implement HTTP basic authentication

  • curl登陆验证
curl -u username=xxx  localhost:3000;

72 Deploy Node apps Basic

  • 简单的重启node和输出log
//example.js

setInterval(function () {
  console.log('Request');
  if(Math.random() < 0.33) {
        throw new Error('boo');
  }
}, 2000)

//输出log

node example.js | tee -a example.log;     //-a表示继续添加,没有则会覆盖

//使用shell

 while true; do node example.js | tee -a example.log ; done

73 Deploy Node apps Ninja Unix like

  • 获取运行程序的pid
//通过运行命令查找
node example.js
pgrep -n -f 'node example.js'   //-f   表示运行的命令

npm start
pgrep -n -f 'nodemon -w 'common/' -w 'server/' -e js,json server/server.js'    //注意要是其实际的运行命令

//通过程序类别查找
ps ax | grep node    //aux能显示更多信息
  • 根据pid查询程序内存使用等信息
//
ps wux pid

//获取部分信息
ps wux pid | awk 'NR>1' | awk '{print 6}'     //|第二行开始|选择第6列

75 Fully take advantage of multi processor servers on deployment

  • 运行多个nodejs服务
//server.js
var http = require('http');

http.createServer(function (req, res) {
  res.end('listeing on :' + process.argv[2]);
}).listen(process.argv[2]);


//命令
node server.js 8001 &   //会返回pid
node server.js 8002 &
node server.js 8003 &
jobs    //返回所有正在运行的node程序pid
var http = require('http');
var httpProxy = require('http-proxy');

var proxy1 = httpProxy.createProxyServer({target:'http://localhost:8080'}).listen(8000);
var proxy2 = httpProxy.createProxyServer({target:'http://localhost:8080'}).listen(8001);
var proxy3 = httpProxy.createProxyServer({target:'http://localhost:8080'}).listen(8002);
var proxy4 = httpProxy.createProxyServer({target:'http://localhost:8080'}).listen(8003);

proxy1.on('proxyReq', function (proxyReq, req, res) {
  console.log('proxy1');
});

proxy2.on('proxyReq', function (proxyReq, req, res) {
  console.log('proxy2');
});

proxy3.on('proxyReq', function (proxyReq, req, res) {
  console.log('proxy3');
});

proxy4.on('proxyReq', function (proxyReq, req, res) {
  console.log('proxy4');
});

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(8080);

76 Support sessions on multiple servers with memcached

  • 在利用nodejs多进程的时候,为确保数据一致(如session),可以使用memcachedmemcached

77 Implement virtual hosting Express

  • 使用express设置虚拟主机
//
var express = require('express');
var master_app = express();
var vhost = require('vhost');

var one = express();
one.get('*', function (req, res) {
  res.end('one');       
});

var two = express();
two.get('*', function (req, res) {
  res.end('two');       
});

var three = express();
three.get('*', function (req, res) {
  res.end('three');     
});

master_app.use(vhost('one', one));
master_app.use(vhost('two', two));
master_app.use(vhost('three', three));

master_app.get('*', function (req, res) {
  res.end('main');
});

master_app.listen(8080);

//访问
curl localhost:8080
curl -H 'Host: one' localhost:8080
curl -H 'Host: two' localhost:8080
curl -H 'Host: three' localhost:8080
  • 修改host名称
sudo vim /etc/hosts

//添加
127.0.0.1 one

//访问
curl one:8080   //直接访问host: one的主机