转载源于blog.csdn.net/sysuzjz
作为前端经常需要模拟后台数据,我们称之为mock。通常的方式为自己搭建一个服务器,返回我们想要的数据。
项目中遇到的请求链接是类似这样子的:www.abc.com/user/login,而不是请求某个文件,如果采用PHP+Apache的方式就需要做路径重写,太麻烦。这里用的是nodejs搭建。
一般来说,请求的链接无非是http或者https的。但有个问题,本人用的mac电脑,在mac和Linux上是不允许绑定1024以下的端口号的。网上的建议是加sudo权限,但实际操作中,80端口可以绑定,443端口绑定失败,提示权限不足,即使我已经用了root账户。最后我是用端口转发(port forward)的方式解决的。
使用环境:
mac os 10.10.5
node v4.2.1
Git version 1.9.5
使用node搭建http服务器:
- 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();
使用node搭建https服务器
https服务器稍微复杂些,需要生成证书,当然这个证书在浏览器看来也是无效的,访问的时候需要添加信任。安装证书需要OpenSSL,这个可以通过安装git来安装,当然也可以自己去安装。
参考http://blog.fens.me/nodejs-https-server/
OpenSSL生成证书
路径替换成自己的路径就好了
- #生成私钥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
根据证书创建https服务器
- 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);
- });
端口转发(Port Forward)
刚才上面两个服务器监听的分别是8080和8443,而我们想要的是80和443。其实也可以直接绑定80和443,用sudo,但不知为何我的电脑加了sudo依旧绑定不了443,所以就找了另一个方法:端口转发。即绑定其他端口,但将80和443端口的请求转发到绑定的端口。
参考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
查看当前所有转发规则:最后的最后,别忘了将请求的地址绑定到本地。将以下添加进hosts:具体添加规则不作阐述了