我搜索了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和其他框架)
发布评论