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 -o filename.type http://localhost:3000 //下载传输过来的文件
curl -X -POST -F(--form) email=example.gmail -F 'username=jinks peng' localhost:3000;
//设置
res.cookie(name, value [, options])
//清除
res.clearCookie(name [, options])
//下载文件
res.download(path [, filename] [, fn])
//发送文件
res.sendFile(path [, options] [, fn])
curl -u username=xxx localhost:3000;
//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
//通过运行命令查找
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能显示更多信息
//
ps wux pid
//获取部分信息
ps wux pid | awk 'NR>1' | awk '{print 6}' //|第二行开始|选择第6列
//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!' + '
' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(8080);
//
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
sudo vim /etc/hosts
//添加
127.0.0.1 one
//访问
curl one:8080 //直接访问host: one的主机