Python的http。server(或SimpleHTTPServer for Python 2)是一个从命令行提供当前目录内容的好方法:

python -m http.server

然而,就网络服务器而言,它是非常缓慢的…

它的行为就像它是单线程的,并且在使用RequireJS加载JavaScript AMD模块时偶尔会导致超时错误。加载一个没有图像的简单页面可能需要5到10秒钟。

还有什么更快更方便的选择吗?


当前回答

如果你安装了PHP,你可以使用内置服务器。

php -S 0:8080

其他回答

使用Servez作为服务器

下载Servez 安装,运行 选择要服务的文件夹 选择“开始” 访问http://localhost:8080或选择“启动浏览器”

注:我之所以把这些放在一起,是因为Chrome将不再支持应用程序,而且我支持那些对命令行没有任何经验的艺术学生

我喜欢实时服务器。它是快速的,有一个很好的实时加载功能,这是非常方便的开发过程中。

用法非常简单:

cd ~/Sites/
live-server

默认情况下,它创建一个IP为127.0.0.1,端口为8080的服务器。

http://127.0.0.1:8080/

如果8080端口没有空闲,它将使用其他端口:

http://127.0.0.1:52749/

http://127.0.0.1:52858/

如果您需要在本地网络中的其他机器上查看web服务器,您可以检查您的IP是什么并使用:

live-server --host=192.168.1.121

下面是一个自动抓取默认接口的IP地址的脚本。它只在macOS上运行。

如果您将它放在.bash_profile中,live-server命令将自动启动具有正确IP的服务器。

# **
# Get IP address of default interface
# *
function getIPofDefaultInterface()
{
    local  __resultvar=$1

    # Get default route interface
    if=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}')
    if [ -n "$if" ]; then
            # Get IP of the default route interface
            local __IP=$( ipconfig getifaddr $if )
            eval $__resultvar="'$__IP'"
    else
        # Echo "No default route found"
        eval $__resultvar="'0.0.0.0'"
    fi
}

alias getIP='getIPofDefaultInterface IP; echo $IP'

# **
# live-server
# https://www.npmjs.com/package/live-server
# *
alias live-server='getIPofDefaultInterface IP && live-server --host=$IP'

我在过去的几年里一直在使用filebrowser,它是我发现的最好的替代品。

我喜欢它的特点:

跨平台:支持Linux、MacOs和Windows(+)。它还支持docker(+)。 下载东西是件轻而易举的事。它可以自动将文件夹转换为zip, tar.gz等,用于传输文件夹。 您可以访问文件或文件夹的每一个用途。

试试polpetta吧…

npm install -g polpetta

然后你就可以

polpetta ~/folder

你已经准备好了:-)

如果使用Mercurial,则可以使用内置的HTTP服务器。在您希望提供的文件夹中:

hg serve

从文档中可以看出:

export the repository via HTTP

    Start a local HTTP repository browser and pull server.

    By default, the server logs accesses to stdout and errors to
    stderr. Use the "-A" and "-E" options to log to files.

options:

 -A --accesslog       name of access log file to write to
 -d --daemon          run server in background
    --daemon-pipefds  used internally by daemon mode
 -E --errorlog        name of error log file to write to
 -p --port            port to listen on (default: 8000)
 -a --address         address to listen on (default: all interfaces)
    --prefix          prefix path to serve from (default: server root)
 -n --name            name to show in web pages (default: working dir)
    --webdir-conf     name of the webdir config file (serve more than one repo)
    --pid-file        name of file to write process ID to
    --stdio           for remote clients
 -t --templates       web templates to use
    --style           template style to use
 -6 --ipv6            use IPv6 in addition to IPv4
    --certificate     SSL certificate file

use "hg -v help serve" to show global options