http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs.php
若是你不想重复的写 nodeJs 的 web-server.js, 则能够使用这个.html
安装 (全局安装加 -g) : node
npm install http-server
若是警告:npm WARN enoent ENOENT: no such file or directory, open 'D:\myserver\package.json'web
解决:这是项目目录下没有package.json这个文件。能够使用npm init -f命令生成一下,至于生成的package.json中缺乏的字段你能够参照其余的模块的package.json文件填进去。至于package.json中的每一个字段的值能够为“”。至于依赖项字段,以安装模块的时候使用-save参数就会自动写到文件中。下面是我测试用的packa.json文件npm
{json
"name": "test",浏览器
"version": "1.0.0",服务器
"description": "23411",cors
"main": "index.js",ide
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"repository": {
"type": "",
"url": ""
}
}
粘贴上:
再"scripts": {
"start": "http-server -a 0.0.0.0 -p 8000",
}
Windows 下使用:
在站点目录下开启命令行输入
http-server
先创建个文件夹如D:\myserver 而后cd
在这个文件夹下启命令行http-server
这时若是报错:不是内部或外部命令,也不是可运行的程序参考资料
解决(来源http://jingyan.baidu.com/article/4f7d5712ce8c1f1a201927fa.html):把计算机属性的环境变量path F:\AppServ\Apache24\bin;F:\AppServ\php5;F:\AppServ\MySQL\bin;C:\Users\Administrator\AppData\Roaming\npm;C:\Program Files\nodejs\;C:\Program Files\nodejs\node_global;C:\Windows\System32;D:\myserver
修改环境变量后不须要重启电脑,只须要从新打开cmd就行
访问: http://localhost:8080 or http://127.0.0.1:8080 就能够访问到index.html文件了
使用于package.json
"scripts": { "start": "http-server -a 0.0.0.0 -p 8000", }
参数 :
-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 打印以上列表并退出