我知道当我想在本地计算机上编写Node.js Web应用程序时,我不需要使用WAMP或MAMP设置本地服务器。 node.js在幕后真的做了什么?我提供此代码来制作一个简单的hello world web应用程序:
class Program
{
static void Main(string[] args)
{
division d = new division();
d.CreateDivisionStructure();
Console.ReadLine();
}
}
class division
{
private static int CountHowManyTimesMyVarWasInitilized = 0;
public division()
{
}
protected string _myVar;
public string myVar
{
get
{
if (_myVar == null)
{
CountHowManyTimesMyVarWasInitilized++;
Console.WriteLine(CountHowManyTimesMyVarWasInitilized);
_myVar = "now myVar is not null";
return _myVar;
}
else
{ return _myVar; }
}
set { _myVar = value; }
}
public void CreateDivisionStructure()
{
Console.WriteLine(myVar);
for (int i = 0; i < 7; i++)
{
Branch b = new Branch(7);
}
}
}
class Branch : division
{
public Branch(bool dImDerivedClass)
{
// constructor for department to prevent recursive stackoverflow if base of department will call the empty constructor
}
public Branch(int NumberOfBranches)
{
Console.WriteLine(myVar);
Department d = new Department(7);
}
}
class Department : Branch
{
public Department(bool ImDerivedClass) : base(true)
{
// constructor for team to prevent recursive stackoverflow if base of Team will call the empty constructor
}
public Department(int numberOfDep) : base(true)
{
for (int i = 0; i < numberOfDep; i++)
{
Console.WriteLine(myVar);
Team t = new Team(7);
}
}
}
class Team : Department
{
public Team(int numberOfTeams) : base(true)
{
for (int i = 0; i < numberOfTeams; i++)
{
Console.WriteLine(myVar);
}
}
}
}
在我的浏览器URL栏中加载&#34; localhost:8080&#34;它有效。
这是如何工作的以及为什么在使用Node.js时我不需要本地服务器?
答案 0 :(得分:3)
你有一台本地服务器......它是你的Node.js应用程序。
当您致电x1 = linspace(0,4*pi, 500); y1 = x^2
时,它会创建一个HTTP服务器。当您在该服务器上调用LoadError: MethodError: `*` has no method matching *(::LinSpace{Float64},
::LinSpace{Float64})
Closest candidates are:
*(::Any, ::Any, !Matched::Any, !Matched::Any...)
*{T}(!Matched::Bidiagonal{T}, ::AbstractArray{T,1})
*(!Matched::Number, ::AbstractArray{T,N})
...
in power_by_squaring at intfuncs.jl:80
in ^ at intfuncs.jl:108
in include_string at loading.jl:282
in include_string at C:\Users\User\.julia\v0.4\CodeTools\src\eval.jl:32
in anonymous at C:\Users\User\.julia\v0.4\Atom\src\eval.jl:84
in withpath at C:\Users\User\.julia\v0.4\Requires\src\require.jl:37
in withpath at C:\Users\User\.julia\v0.4\Atom\src\eval.jl:53
[inlined code] from C:\Users\User\.julia\v0.4\Atom\src\eval.jl:83
in anonymous at task.jl:58
while loading C:\Users\User\Desktop\Comp Sci\Class\plotTest, in expression
starting on line 7
时,它会绑定到请求的端口和可选的请求地址,并侦听连接。当数据进入这些连接时,它会像任何其他HTTP服务器一样响应。
HTTP服务器使用您的请求/响应回调,每当有效的HTTP请求进入时都会触发它。
答案 1 :(得分:2)
因为节点开箱即用,您需要运行网络服务器所需的所有库,所以您正在使用的http库打开8080端口并使用您提供的功能处理请求
这部分:
function(request,response){
response.writeHead(200, {"content-type":"text/html"});
response.write("hello world");
response.end();
}
答案 2 :(得分:0)
不,你不需要它。因为节点本身可以是您的网络服务器,就像在您的示例中一样。 Node是基于V8构建的,它是chrome JavaScript引擎。
看一下Express js模块,它提供了很多开箱即用的功能