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
- 使用node-http-proxy作为代理
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
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的主机
nodejs review-03的更多相关文章
- nodejs复习03
文件系统fs 重命名 fs.rename() fs.renameSync 优先选择异步,可以进行异常判断 打开关闭文件 fd = fs.openSync(file, flags) fs.closeSy ...
- nodeJs学习-03 GET数据请求,js拆解/querystring/url
原生JS解析参数: const http = require('http'); http.createServer(function(req,res){ var GET = {}; //接收数据容器 ...
- nodejs+websocket制作聊天室视频教程
本套教程主要讲解了node平台的安装,node初级知识.node 服务器端程序响应http请求,通过npm安装第三方包,websocket即时通讯.聊天页面界面制作.拖动原理.拖动效果.遮罩效果.定位 ...
- [Java 教程 03] 我的第一个Java程序
现在,大家应该都已经安装好jdk环境了吧!是不是已经跃跃欲试,按耐不住心中的小激动了?那我们现在就来写我们java学习生涯中的第一个java程序. 文件相关设置 为了方便后面大家的学习呢?有一点大家还 ...
- 【NodeJS 学习笔记03】先运行起来再说
前言 最近同事推荐了一个不错的网址:https://github.com/nswbmw/N-blog/wiki/_pages 里面的教程很是详细,我们现在跟着他的节奏学习下NodeJS,一个简单的博客 ...
- nodejs随记03
文件操作 文件系统的操作 fs.readFile(filename, [options], callback) fs.writeFile(filename, data, [options], call ...
- Nodejs in Visual Studio Code 03.学习Express
1.开始 下载源码:https://github.com/sayar/NodeMVA Express组件:npm install express -g(全局安装) 2.ExpressRest 打开目录 ...
- Angular4+NodeJs+MySQL 入门-03 后台接口定义
这篇里是如何定义接口,我们一般访问接口如:post请求调用http://127.0.0.1:11000/webapi/userinfo/user 这个接口,成功返回用户信息,如果失败要返回失败原因等. ...
- [Intel Edison开发板] 04、Edison开发基于nodejs和redis的服务器搭建
一.前言 intel-iot-examples-datastore 是Intel提供用于所有Edison开发板联网存储DEMO所需要的服务器工程.该工程是基于nodejs和redis写成的一个简单的工 ...
- nodeJs 5.0.0 安装配置与nodeJs入门例子学习
新手学习笔记,高手请自动略过 安装可以先看这篇:http://blog.csdn.net/bushizhuanjia/article/details/7915017 1.首先到官网去下载exe,或者m ...
随机推荐
- bzoj 1065: [NOI2008] 奥运物流
1065: [NOI2008] 奥运物流 Description 2008北京奥运会即将开幕,举国上下都在为这一盛事做好准备.为了高效率.成功地举办奥运会,对物流系统 进行规划是必不可少的.物流系统由 ...
- Kakfa重连测试
在Kafak已启动的情况下: 发送端首次连接大概耗时400毫秒.后续消息发送都在1毫秒以下. 接收端首次连接大概耗时400-7000毫秒.后续消息接收都在1毫秒以下.(具体时间与topic中存留的消息 ...
- asp.net mvc 通过T4模板生成框架
http://www.cnblogs.com/rdst/archive/2012/08/13/2637210.html http://www.kuqin.com/shuoit/20140716/341 ...
- sql 的实用函数(包含日期函数、截取字符串函数)
CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用不同的格式显示日期/时间数据. 语法 CONVERT(data_type(length),data_to_b ...
- python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...
- Linux--niaoge
鸟哥的Linux私房菜 网络基础: 那 TCP/IP 是如何运作的呢?我们就拿妳常常连上的 Yahoo 入口网站来做个说明好了,整个联机的状态可以这样看: 应用程序阶段:妳打开浏览器,在浏览器上面输入 ...
- Apache Shiro 学习记录1
最近几天在学习Apache Shiro......看了一些大神们的教程.....感觉收获不少.....但是毕竟教程也只是指引一下方向....即使是精品教程,仍然有很多东西都没有说明....所以自己也稍 ...
- mysql中event的用法详解
一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...
- [Head First设计模式]身边的设计模式——适配器模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- [Python] Python中的一些特殊函数
1. 过滤函数filter 定义:filter 函数的功能相当于过滤器.调用一个布尔函数bool_func来迭代遍历每个列表中的元素:返回一个使bool_func返回值为true的元素的序列. a=[ ...