如何创build一个本地主机服务器来运行一个AngularJS项目

我已经使用Xampp和JetBrain WebStorm来运行一个AngularJS项目。 但它是复杂和低性能。是否有其他方式来运行AngularJS项目?

如果你正在运行node.js http-server是非常容易的。 安装: npm install -g http-server 。 安装完毕后,进入你的项目文件夹并运行http-server -o-o是打开浏览器到页面。

Python有一个专门用于启动web服务器的内置命令:

 python -m SimpleHTTPServer 8000 

将在端口8000上启动一个networking服务器

(Python是这个的先决条件,如果你没有安装python,其他答案可能会更容易)

您可以从terminal或cmd安装Node.js开始:

 apt-get install nodejs-legacy npm 

然后安装依赖关系:

 npm install 

然后,启动服务器:

 npm start 

“假设你已经安装了nodejs”,

mini-http是创buildhttp服务器的一个非常简单的命令行工具,
全局安装包npm install mini-http -g
然后使用你的cmd(terminal)在你的项目目录中运行mini-http -p=3000并繁荣! 你在端口3000上创build一个服务器,现在去检查http:// localhost:3000

注意:指定一个端口不是必需的,你可以简单的运行mini-http或者mh来启动服务器

cd <your project folder> (你的angularjs的可部署代码在那里)

sudo npm install serve -g

服务

你可以打你的网页

localhost:3000或IP地址:3000

如果你是一个Java人,简单的把你的angular度文件夹放在你的web应用程序的web内容文件夹中,并部署到你的tomcat服务器上。 超级简单!

假设您已经安装了node.js,则可以使用浏览器同步进行同步浏览器testing。

 服务 

这个命令在你的terminal运行后,在~/my-app$类的项目文件夹中运行

  • 然后运行命令 – 它会显示URl NG Live Development Server正在localhost:4200上侦听

  • 在http:// localhost:4200上打开浏览器

您还可以在Visual Studio代码中设置环境。 运行Ctrl + Shift + P,然后在出现的框中inputctr并select任务:configurationTask Runner,然后将task.json文件更改为: { "version": "0.1.0", "command": "explorer", "windows": { "command": "explorer.exe" }, "args": ["index.html"] } ,保存您的更改,然后select您的index.html文件并键入Ctrl + Shift + B。这将使用默认浏览器打开项目。

使用local-web-server npm包。

https://www.npmjs.com/package/local-web-server

 $ npm install -g local-web-server $ cd <your-app-folder> $ ws 

另外,你可以运行

 $ ws -p 8181 

-p定义您要使用的端口

之后,只需进入您的浏览器并访问http:localhost:8181 /

我用:

  • expression和
  • 摩根

安装Node.js 和npm。 npm与Node.js一起安装

放置在根项目目录中

 $ cd <your_angularjs_project> 

下一个命令创buildpackage.json

 $ npm init 

安装快速==>节点的快速,非select性,极简主义:

 $ npm install express --save 

安装morgan ==> node.js的HTTP请求logging器中间件

 $ npm install morgan --save 

创build文件server.js

在server.js文件中添加下面的代码

 // Required Modules var express = require("express"); var morgan = require("morgan"); var app = express(); var port = process.env.PORT || 3002; app.use(morgan("dev")); app.use(express.static("./")); app.get("/", function(req, res) { res.sendFile("./index.html"); //index.html file of your angularjs application }); // Start Server app.listen(port, function () { console.log( "Express server listening on port " + port); }); 

最后在localhost服务器上运行你的AngularJS项目:

 $ node server.js