javascript 从 http://localhost/ 运行 file://
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32332485/
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
Run a file:// from http://localhost/
提问by KDX2
I wonder to know how can I make my .html project run not from file:// but as a localhost because one of the functions I've implemented requires getUserMedia which browsers instantly block, when loading from file://. I've done a lot of research on this but I'm still not understanding how it should be done, so if you can, please explain it in detail if it's not too pretentious of me, of course.
我想知道如何让我的 .html 项目不是从 file:// 运行而是作为本地主机运行,因为我实现的功能之一需要 getUserMedia,当从 file:// 加载时,浏览器会立即阻止。我对此做了很多研究,但我仍然不明白应该如何做,所以如果可以的话,当然如果不是我太自命不凡,请详细解释一下。
采纳答案by Wargog
If you are on Ubuntu (or any other similar Linux distro) run
如果您使用的是 Ubuntu(或任何其他类似的 Linux 发行版),请运行
apt-get install apache2
then move your files to the /var/www/html folder, and navigate to localhost in your browser.
然后将您的文件移动到 /var/www/html 文件夹,并在浏览器中导航到 localhost。
On Windows, you can install XAMPP, then move your files to wherever you installed it to in the \htdocs
folder
在 Windows 上,您可以安装XAMPP,然后将文件移动到\htdocs
文件夹中的任何安装位置
Hope this helps, thanks.
希望这有帮助,谢谢。
回答by corn3lius
install node js
安装节点js
npm install -g http-server
from the directory containing html files.
从包含 html 文件的目录中。
http-server ./ -p 80
reference https://github.com/indexzero/http-server
回答by Hassan AlMandil
I use live-serveron my mac by running the below code on the command line from inside the folder containing your index.html:
我通过在包含 index.html 的文件夹内在命令行上运行以下代码来在我的 mac 上使用live-server:
live-server --port=8000
In addition to running index.html from localhost, live-server automatically reloads the page after any changes made to files that affect the dome for that page (i.e. .js or .css etc..) which can accelerate development.
除了从本地主机运行 index.html 之外,live-server 会在对影响该页面的圆顶的文件(即 .js 或 .css 等)进行任何更改后自动重新加载页面,这可以加速开发。
Installation
安装
You should probably install this globally.
您可能应该全局安装它。
npm install -g live-server
note:
- You need node.jsin order to use npm.
- --port= can be any free port and not nessessarly 8000.
笔记:
- 你需要node.js才能使用 npm。
- --port= 可以是任何自由端口,而不是 8000。
回答by runningmark
python -m SimpleHTTPServer 8000
python -m SimpleHTTPServer 8000
And you are good to go!
你很高兴去!
Start a server within seconds!
在几秒钟内启动服务器!
Note you do require an installed python
请注意,您确实需要安装 python