nodejs一行命令快速启动一个web服务(两大神器:anywhere,http-server)

嗨,zaga好啊,我是临山,今天我们就来学学“一个命令快速启动一个web服务”,两个npm包都可以实现,下面一一道来。

有时候我们在写一个简单的html测试代码的时候,需要使用一个web服务,通过浏览器:localhost:xxxx/index.html 访问自己的测试demo,在没有node的情况下,往往需要搭建Apache或者Tomcat服务(也有使用php等搭建),在此,在下分享最简单的方法,这也是node带给我们极大的方便之处。

关于node的安装,可直接下载node的安装包,双击安装即可,非常简单,大家可以直接查看node官网。

进入正题,一行命令在项目文件根目录启动一个server

方法一(anywhere)

安装

anywhere 安装非常简单,在安装了node,npm的前提下

npm install -g anywhere
复制代码

使用

安装完成之后直接在想要启动服务的文件夹下,打开命令行,输入anywhere回车即可启动。
指定启动端口:

anywhere 8888 
复制代码

则会在8888端口启动


方法二(http-serve)

安装

npm install http-server -g
复制代码

使用

安装完成之后直接在想要启动服务的文件夹下,打开命令行,输入http-server,即可开启


在浏览器输入上面显示的ip可端口即可访问。

http-server -c-1 也可启动,表示在禁止缓存情况下启用

与 Python 的 SimpleHTTPServer,Node.js 的 serve 不同,serve2 是一个支持动态文件的工具,前者们只支持静态文件,而serve2支持编写代码动态处理和返回http请求。 常常我们可能会有这样的需求场景:无论是在开发时还是尝试某个框架时,往往需要处理请求参数、Cookies、Http headers等,这样简单的静态文件服务器便无法满足这种需求了。 于是serve2便应运而生了,基于Tj的serve开发,使用起来跟一般的静态文件服务器相同,执行一条命令即可。详细的用法介绍如下。 1. 下载安装 npm install -g serve2 2. 使用运行 λ serve2 --help   Usage: serve2 [options] [dir]   Options:     -h, --help                output usage information     -V, --version             output the version number     -a, --auth <user>:<pass>  specify basic auth credentials     -F, --format <fmt>        specify the log format string     -p, --port <port>         specify the port [3000]     -H, --hidden              enable hidden file serving     -S, --no-stylus           disable stylus rendering     -J, --no-jade             disable jade rendering         --no-less             disable less css rendering     -I, --no-icons            disable icons     -L, --no-logs             disable request logging     -D, --no-dirs             disable directory serving     -f, --favicon <path>      serve the given favicon     -M, --mocks <path>        mock files directory         --cookies             add cookies parse support     -C, --cors                allows cross origin access serving         --compress            gzip or deflate the response         --exec <cmd>          execute command on each request 标签:serve2 分享 window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "2", "bdMiniList": [], "bdPic": "", "bdStyle": "1", "bdSize": "24" }, "share": {} }; with (document)0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' ~(-new Date() / 36e5)];\r\n \r\n \r\n \r\n \r\n \u8f6f\u4ef6\u9996\u9875\r\n \u8f6f\u4ef6\u4e0b\u8f7d\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\nwindow.changyan.api.config({\r\nappid: 'cysXjLKDf', conf: 'prod_33c27aefa42004c9b2c12a759c851039' });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值