做为前端常常须要模拟后台数据,咱们称之为mock。一般的方式为本身搭建一个服务器,返回咱们想要的数据。javascript
项目中遇到的请求连接是相似这样子的:www.abc.com/user/login,而不是请求某个文件,若是采用PHP+Apache的方式就须要作路径重写,太麻烦。这里用的是nodejs搭建。前端
通常来讲,请求的连接无非是http或者https的。但有个问题,本人用的mac电脑,在mac和Linux上是不容许绑定1024如下的端口号的。网上的建议是加sudo权限,但实际操做中,80端口能够绑定,443端口绑定失败,提示权限不足,即便我已经用了root帐户。最后我是用端口转发(port forward)的方式解决的。java
使用环境:node
mac os 10.10.5git
node v4.2.1json
git version 1.9.5浏览器
var http = require("http"), url = require("url"); function start() { function onRequest(request, response) { // 获取请求路径 var pathname = url.parse(request.url).pathname; // 关闭nodejs 默认访问 favicon.ico if (!pathname.indexOf('/favicon.ico')) { return; }; // 返回数据 response.writeHead(200, {"Content-type": "text/plain"}); // 路由 switch(pathname) { case '/': response.write('index'); break; case '/user/login': response.write(JSON.stringify({ 'code': 200, 'msg': success })); break; case '/user/logout': response.write(JSON.stringify({ 'code': 200, 'msg': success })); break; default: response.write('default'); break; } response.end(); } http.createServer(onRequest).listen(8080); console.log("Server has start!"); } start();
https服务器稍微复杂些,须要生成证书,固然这个证书在浏览器看来也是无效的,访问的时候须要添加信任。安装证书须要OpenSSL,这个能够经过安装git来安装,固然也能够本身去安装。服务器
参考http://blog.fens.me/nodejs-https-server/tcp
#生成私钥key文件: your_path > penssl genrsa -out privatekey.pem 1024 Generating RSA private key, 1024 bit long modulus ...........................++++++ ........++++++ e is 65537 (0x10001) #经过私钥生成CSR证书签名 your_path > openssl req -new -key privatekey.pem -out certrequest.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:Beijing Locality Name (eg, city) []:Beijing Organization Name (eg, company) [Internet Widgits Pty Ltd]:fens.me Organizational Unit Name (eg, section) []:fens.me Common Name (eg, YOUR name) []:Conan Zhang Email Address []:bsspirit@gmail.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: # 经过私钥和证书签名生成证书文件 your_path > openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem Signature ok subject=/C=CN/ST=Beijing/L=Beijing/O=fens.me/OU=fens.me/CN=Conan Zhang/emailAddress=bsspirit@gmail.com
var https = require('https'), url = require("url"), fs = require("fs"); var options = { key: fs.readFileSync('./privatekey.pem'), cert: fs.readFileSync('./certificate.pem') }; function onRequest(request, response) { // 获取请求路径 var pathname = url.parse(request.url).pathname; // 关闭nodejs 默认访问 favicon.ico if (!pathname.indexOf('/favicon.ico')) { return; }; // 收到来自 pathname 的请求 console.log("Request for " + pathname + " received."); // 返回数据 response.writeHead(200, {"Content-type": "text/json"}); response.write('hello world'); response.end(); } https.createServer(options, onRequest).listen(8443, function () { console.log('Https server listening on port ' + 8443); });
刚才上面两个服务器监听的分别是8080和8443,而咱们想要的是80和443。其实也能够直接绑定80和443,用sudo,但不知为什么个人电脑加了sudo依旧绑定不了443,因此就找了另外一个方法:端口转发。即绑定其余端口,但将80和443端口的请求转发到绑定的端口。ui
参考http://salferrarello.com/mac-pfctl-port-forwarding/
将如下代码贴进命令行执行
echo " rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080 rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port 8443 " | sudo pfctl -ef -这段代码的意思是将80端口的请求转发到8080,将443端口的请求转发到8443。
执行完以后命令行会提示*** disabled,能够没必要理会。
须要解除转发的话,在命令行贴如下代码:
sudo pfctl -F all -f /etc/pf.conf
sudo pfctl -s nat
127.0.0.1 www.abc.com具体添加规则不做阐述了