如何使用服务器运行index.html(针对angularjs和其他框架)

编程入门 行业动态 更新时间:2024-10-08 14:38:12
本文介绍了如何使用服务器运行index.html(针对angularjs和其他框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我搜索了stackoverflow和一些论坛,找不到直接的解决方案,后来我遇到了一些可以正常工作的ans,所以我将其发布在这里:)

I have searched stackoverflow and some forums and couldn't find a direct solution and later I came across some ans which works fine so I'm posting it here :)

ans用于以下文件夹结构,您也可以根据自己的文件夹结构对其进行自定义.

The ans is for the below folder structure and you can also customize it for your folder structure.

--project ---app ----js ----services ----(...) ----index.html

请参考以下答案. 如果您有更好的方法请发表,也可以添加一些评论以使答案更好. 谢谢.

Please refer the answer below. Please post if you have better way to do it and also you can add some comments to make the answer better. Thanks.

推荐答案

方法1:

使用node.js运行index.html文件复制,将以下代码粘贴到您的应用文件夹(高于层次结构)中的server.js文件中

Method 1:

Using node.js to run index.html file copy paste the below code to server.js file in your app folder(above hierarchy)

var http = require('http'); var fs = require("fs"); http.createServer(function(request, response) { if(/(.*?).css$/.test(request.url.toString())){ sendFileContent(response, request.url.toString().substring(1), "text/css"); }else if(/(.*?).js$/.test(request.url.toString())){ sendFileContent(response, request.url.toString().substring(1), "text/javascript"); }else if(/(.*?).html$/.test(request.url.toString())){ sendFileContent(response, request.url.toString().substring(1), "text/html"); }else if(request.url.toString().substring(1) == ''){ sendFileContent(response, "index.html", "text/html"); } }).listen(3000); function sendFileContent(response, fileName, contentType){ fs.readFile(fileName, function(err, data){ if(err){ response.writeHead(404); response.write("Not Found!"); } else{ response.writeHead(200, {'Content-Type': contentType}); response.write(data); } response.end(); }); }

,然后在app文件夹中运行node server.js. 您的html文件将在localhost:3000

and from the app folder run node server.js. Your html file will be serving in localhost:3000

使用http服务器.请按照此链接中的步骤操作,以全局方式安装HTTP服务器,并从您的应用程序文件夹中运行指令 http-server -a localhost -p 8000 -c-1 ./app

Using http-server. Follow the steps in this link to install http-server globally and from your app folder run cmd http-server -a localhost -p 8000 -c-1 ./app

,您的index.html文件将在localhost:8000

and your index.html file will be serving in localhost:8000

注意:您可以在上述方法中的.listen和-p中更改端口号.

Note: You can change the port number in .listen and -p in above methods.

更多推荐

如何使用服务器运行index.html(针对angularjs和其他框架)

本文发布于:2023-11-23 21:37:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622925.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   框架   服务器   index   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!