Nodejs-npm包之http-server和json-server
来源:互联网 发布:工期优化步骤 编辑:程序博客网 时间:2024/05/22 05:18
Nodejs安装
去http://nodejs.cn/下载即可
Windows/Linux都行,此处省略
不懂的可以看如下传送门:
http://www.runoob.com/nodejs/nodejs-install-setup.html
安装cnpm
国内的npm网速实在是捉急,也多亏了阿里巴巴,可以使用阿里巴巴的cnpm
建议使用-g全局安装,这样在别的地方也可以使用该命令 $ npm install -g cnpm --registry=https://registry.npm.taobao.org
安装完毕后,可直接使用cnpm代替npm命令,cnpm会优先去alibaba里面的CDN找有没有这个文件,没有的话,再帮你转换成npm命令
npm插件
json-server
https://github.com/typicode/json-server
安装:
cnpm install -g json-server
安装完毕后,创建一个json文件,如:db.json
{ "posts": [ { "id": 1, "title": "json-server", "author": "typicode" } ], "comments": [ { "id": 1, "body": "some comment", "postId": 1 } ], "profile": { "name": "typicode" }}
启动 $ json-server --watch db.json
浏览器访问:http://localhost:3000/posts/1
结果:
{ "id": 1, "title": "json-server", "author": "typicode" }
修改端口
端口是使用了express的默认端口,修改端口 $ json-server --watch db.json --port 3004
结果npm的init以后在package.json一起使用,则是直接在package.json改成
{ "name": "rest-api-demo", "version": "1.0.0", "description": "Nick's NPM APP", "main": "index.js", "scripts": { "server": "json-server db.json", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Nick", "license": "ISC"}
只需要在”scripts”中加上"server": "json-server db.json"
优点:
前端不需要等后端的json才能测试,直接自己本地构建一个json的server即可
能够自动根据json文件构建基于RESTFUL接口,与后端天然对应
http-server
http-server也是类似json-server,只不过http-server是用来将本地的文件转成文件
https://github.com/indexzero/http-server
安装: cnpm install http-server -g
启动: http-server
浏览器可访问http://localhost:8080/
常用参数:
-p 端口号 (默认 8080)-a IP 地址 (默认 0.0.0.0)-d 显示目录列表 (默认 'True')-i 显示 autoIndex (默认 'True')-e or --ext 如果没有提供默认的文件扩展名(默认 'html')-s or --silent 禁止日志信息输出--cors 启用 CORS via the Access-Control-Allow-Origin header-o 在开始服务后打开浏览器-c 为 cache-control max-age header 设置Cache time(秒) , e.g. -c10 for 10 seconds (defaults to '3600'). 禁用 caching, 则使用 -c-1.-U 或 --utc 使用UTC time 格式化log消息-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com-S or --ssl 启用 https-C or --cert ssl cert 文件路径 (default: cert.pem)-K or --key Path to ssl key file (default: key.pem).-r or --robots Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')-h or --help 打印以上列表并退出
- Nodejs-npm包之http-server和json-server
- Nodejs之npm&package.json
- nodejs 创建http server
- NodeJS http Server
- nodejs 之npm包管理工具详解
- Nodejs 包与 NPM 第三方模块安装和 package.json 以及 CNPM
- Nodejs实现简单http server
- Nodejs http server "Hello world"
- NPM--nodejs包管理
- nodejs包管理器npm
- client提交json(nodejs)和server解析json并返回json数据(express)
- HTTP Server和Application Server
- Multi-Core HTTP Server with NodeJS
- Win10 Nodejs搭建http-server注意点
- Apache Http Server和Tomcat 之区别
- Apache Http Server和Tomcat 之区别
- Apache Http Server和Tomcat 之区别
- Apache Http Server和Tomcat 之区别
- 复习css布局模型
- 宝宝跟随、宠物跟随主人
- JSP基础语法
- Watto and Mechanism CodeForces
- ubuntu16.04和win10安装anaconda以及path配置
- Nodejs-npm包之http-server和json-server
- 《JS高级程序设计》第6章读书笔记:创建对象(一)之工场模式和构造函数模式
- 成为JavaGC专家(2)—如何监控Java垃圾回收机制
- SurfaceView的概念和优点及使用
- 小小粉丝度度熊--尺取法、合并一些重复的区间。
- Android.mk简单分析
- Go指南练习之《斐波纳契闭包》
- Maven+Spring构建Demo
- HBase开启Snappy压缩