iOS 搭建本地服务器

1. 打开终端,开启Apache:

//开启apache:  sudo apachectl start

//重启apache:  sudo apachectl restart

//关闭apache:  sudo apachectl stop

回车会提示输入密码,也就是你电脑的密码,http://127.0.0.1/测试一下,成功则如

下图:

成功开启Apache

2. 点击Finder,然后Command+Shift+G,前往Apache服务器的文件路径(/Library/WebServer/Documents),如图:

Apache服务器文件路径

在步骤1中只输入一个http://127.0.0.1其实默认打开的是index.html.en(html是一个网页文件),该文件的内容就是在步骤1中测试时浏览器所显示的内容。此时如果我在浏览器的网址框输入的是http://127.0.0.1/PoweredByMacOSX.gif, 浏览器就便会显示PoweredByMacOSX.gif图片,如果没有正常显示,提示说没有权限时,单击该文件,然后Command+I在末尾设置权限即可

3. 测试

创建一个文件,如test.html(名字能够随意起),

接下来用浏览器访问http://127.0.0.1/test.html

IP(127.0.0.1)也可以换成你电脑的IP地址,这样在同一局域网的设备也可以访问服务器的内容。

配置文件路径为

/etc/apache2/httpd.conf

PS:使用过后,记得关闭服务器,要不然会一直消耗你电脑内存,后果你懂的。


Mac下配置Apache服务器

地址:https://www.cnblogs.com/wanxudong/p/5846907.html

地址:https://jingyan.baidu.com/article/922554467d763b851648f4dc.html

地址:https://www.cnblogs.com/silence-wzx/p/5137766.html


二.用JSON-server模拟数据

1.安装

  sudo npm install json-server -g

安装完成后可以用 json-server -h 命令检查是否安装成功,成功后会出现

json-server [options] <source>

选项:

--config, -c Path to config file [默认值: "json-server.json"]

--port, -p Set port [默认值: 3000]

--host, -H Set host [默认值: "0.0.0.0"]

--watch, -w Watch file(s) [布尔] --routes,

-r Path to routes file --static,

-s Set static files directory --read-only,

--ro Allow only GET requests [布尔] --no-cors,

--nc Disable Cross-Origin Resource Sharing [布尔]

--no-gzip, --ng Disable GZIP Content-Encoding [布尔]

--snapshots, -S Set snapshots directory [默认值: "."]

--delay, -d Add delay to responses (ms) --id,

-i Set database id property (e.g. _id) [默认值: "id"]

--quiet, -q Suppress log messages from output [布尔]

--help, -h 显示帮助信息 [布尔] --version,

-v 显示版本号 [布尔]

示例: json-server db.json

json-server file.js

json-server http://example.com/db.json

https://github.com/typicode/json-server

2.运行

安装完成后,可以在任一目录下建立一个 xxx.json文件,例如在 mock/ 文件夹下,建立一个 db.json 文件,并写入以下内容,并在 mock/ 文件夹下执行

json-server db.json -p 3003

{"news":[ {"id":1,"title":"曹县宣布昨日晚间登日成功","date":"2016-08-12","likes":55,"views":100086}, {"id":2,"title":"长江流域首次发现海豚","date":"2016-08-12","likes":505,"views":9800} ],"comments":[ {"id":1,"news_id":1,"data": [    {"id":1,"content":"支持党中央决定"},    {"id":2,"content":"抄写党章势在必行!"} ] }]}

3.操作数据

使用【GET 接口】查询数据这个时候访问http://localhost:3003/db,可以查看 db.json文件中所定义的全部数据。使用浏览器地址栏,jQuery.get 或 fecth({method: "get"}) 访问http://localhost:3003/news则可以看到 news

对象下的数据,以Array格式返回:

[ {"id":1,"title":"曹县宣布昨日晚间登日成功","date":"2016-08-12","likes":55,"views":100086}, {"id":2,"title":"长江流域首次发现海豚","date":"2016-08-12","likes":505,"views":9800}]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。