如何使用服务器运行index.html(针对angularjs和其他框架) [英] How to run index.html using a server(for angularjs and other frameworks)

查看:160
本文介绍了如何使用服务器运行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和其他框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆