Javascript 如何在本地主机上运行 html 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38497334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run html file on localhost?
提问by Volkan ?ahin
I have a html file and i run it on localhost. But, this file includes mirror using webcam. Example, how can i run this html fileon localhost? Webcam starting on this example when check to live checkbox. Thanx for your reply.
我有一个 html 文件,我在本地主机上运行它。但是,此文件包含使用网络摄像头的镜像。例如,我如何在本地主机上运行这个 html 文件?WebCam在此示例中启动时检查到实时复选框。感谢您的回复。
回答by Clinton Roy
You can run your file in http-server.
您可以在http-server 中运行您的文件。
1> Have Node.jsinstalled in your system.
1>在您的系统中安装Node.js。
2> In CMD, run the command npm install http-server -g
2>在CMD中,运行命令npm install http-server -g
3> Navigate to the specific path of your file folder in CMD and run the command http-server
3> 在 CMD 中导航到您的文件夹的特定路径并运行命令http-server
4> Go to your browser and type localhost:8080. Your Application should run there.
4> 转到浏览器并输入localhost:8080。您的应用程序应该在那里运行。
Thanks:)
谢谢:)
回答by Nora
You can use python -m http.server
. By default the local server will run on port 8000. If you would like to change this, simply add the port number python -m http.server 1234
您可以使用python -m http.server
. 默认情况下,本地服务器将在端口 8000 上运行。如果您想更改此设置,只需添加端口号python -m http.server 1234
If you are using python 2 (instead of 3), the equivalent command is python -m SimpleHTTPServer
如果您使用的是 python 2(而不是 3),则等效的命令是 python -m SimpleHTTPServer
回答by MrE
If you are running Python3, you may want to instead try:
如果您正在运行 Python3,您可能想尝试:
python -m http.server
See this answer.
看到这个答案。
回答by Anuj Gautam
As Nora suggests, you can use the python simple server.
Navigate to the folder from which you want to serve your html page, then execute python -m SimpleHTTPServer
.
Now you can use your web-browser and navigate to http://localhost:8000/where your page is being served.
If your page is named index.html then the server automatically loads that for you. If you want to access any other page, you'll need to browse to http://localhost:8000/{your page name}
正如 Nora 建议的那样,您可以使用 python 简单服务器。导航到要提供 html 页面的文件夹,然后执行python -m SimpleHTTPServer
. 现在您可以使用您的网络浏览器并导航到您的页面所在的http://localhost:8000/。如果您的页面名为 index.html,那么服务器会自动为您加载该页面。如果你想访问任何其他页面,你需要浏览到http://localhost:8000/{your page name}
回答by Ritu Gupta
Install Node js - https://nodejs.org/en/
go to folder where you have html file:
- In CMD, run the command to install http server-
npm install http-server -g
- To load file in the browser run -
http-server
- In CMD, run the command to install http server-
If you have specific html file. Run following command in CMD.-
http-server fileName
by default port is
8080
Go to your browser and type
localhost:8080
. Your Application should run there.If you want to run on different port:
http-server fileName -p 9000
安装 Node js - https://nodejs.org/en/
转到您拥有 html 文件的文件夹:
- 在CMD中,运行命令安装http服务器-
npm install http-server -g
- 要在浏览器中加载文件,请运行 -
http-server
- 在CMD中,运行命令安装http服务器-
如果您有特定的 html 文件。在 CMD 中运行以下命令。-
http-server fileName
默认端口是
8080
转到您的浏览器并键入
localhost:8080
. 您的应用程序应该在那里运行。如果你想在不同的端口上运行:
http-server fileName -p 9000
Note : To run your .js file run:
node fileName.js
注意:要运行 .js 文件,请运行:
node fileName.js
回答by HamzaMushtaq
You can install Xampp and run apache serve and place your file to www folder and access your file at localhost/{file name} or simply at localhost if your file is named index.html
您可以安装 Xampp 并运行 apache serve 并将您的文件放置到 www 文件夹并在 localhost/{file name} 或仅在 localhost(如果您的文件名为 index.html)访问您的文件
回答by HamzaMushtaq
You can try installing one of the following localhost softwares:
您可以尝试安装以下本地主机软件之一:
- xampp
- wamp
- ammps server
- laragon
- xampp
- 瓦普
- 放大器服务器
- 拉贡
There are many more such softwares but the best among them are the ones mentioned above. they also allow domain names (for example: example.com
)
还有更多这样的软件,但其中最好的是上面提到的那些。它们还允许域名(例如:example.com
)