Javascript 如何使用 Node.js 运行用 js 编写的服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14154357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:51:16  来源:igfitidea点击:

How to run server written in js with Node.js

javascriptnode.js

提问by Rupesh Patel

I have installed node.js from here http://nodejs.org/. in my windows8 machine. copied the example server code in my server.js file

我已经从这里安装的node.js http://nodejs.org/。在我的 windows8 机器上。在我的 server.js 文件中复制了示例服务器代码

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

then opened the node.js prompt and written node c:/node/server.js but nothing happens. I am a php developer just trying hands on it, any guidelines will really be helpful.

然后打开 node.js 提示并写入 node c:/node/server.js 但没有任何反应。我是一名 php 开发人员,只是在尝试使用它,任何指导方针都会非常有帮助。

回答by Peter Krejci

You don't need to go in node.js prompt, you just need to use standard command promt and write

你不需要进入 node.js prompt,你只需要使用标准命令 promt 并编写

node c:/node/server.js

this also works:

这也有效:

node c:\node\server.js

and then in your browser:

然后在您的浏览器中:

http://localhost:1337

回答by Colonel Panic

Nodejs is a scripting language (like Python or Ruby, and unlike PHP or C++). To run your code, you need to enter a command in the terminal / shell / command prompt. Look for an application shortcut in your operating system by one of those names.

Nodejs 是一种脚本语言(类似于 Python 或 Ruby,与 PHP 或 C++ 不同)。要运行您的代码,您需要在终端 / shell / 命令提示符中输入命令。通过这些名称之一在您的操作系统中查找应用程序快捷方式。

The command to run in the terminal will be

在终端中运行的命令将是

node server.js

But you will first need to browse in the terminal to the same folder as the file server.js. The syntax for using the terminal varies by operating system, look for its documentation.

但是您首先需要在终端中浏览到与文件相同的文件夹server.js。使用终端的语法因操作系统而异,请查找其文档。

回答by Fil

I open a text editor, in my case I used Atom. Paste this code

我打开一个文本编辑器,在我的例子中我使用了 Atom。粘贴此代码

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

and save as

并另存为

helloworld.js

in

c:\xampp\htdocs\myproject 

directory. Next I open node.js commamd prompt enter

目录。接下来我打开 node.js 命令提示符回车

cd c:\xampp\htdocs\myproject

next

下一个

node helloworld.js

next I open my chrome browser and I type

接下来我打开我的 chrome 浏览器并输入

http://localhost:1337

and there it is.

就在那里。

回答by Ayaz Ali Khatri

Just go on that directory of your JS file from cmdand write node jsFile.jsor even node jsFile; both will work fine.

只需从你的 JS 文件的那个目录开始cmd,写node jsFile.js甚至node jsFile; 两者都可以正常工作。

回答by Abhijeet

Just try

你试一试

node server

from cmd prompt in that directory

从该目录中的 cmd 提示符

回答by Jeff

If you are in a Linux container, such as on a Chromebook, you will need to manually browse to your localhost's address. I am aware the newer Chrome OS versions no longer have this problem, but on my Chromebook, I still had to manually browse to the localhost's address for your code to work.

如果您在 Linux 容器中,例如在 Chromebook 上,您将需要手动浏览到您的本地主机地址。我知道较新的 Chrome OS 版本不再有这个问题,但在我的 Chromebook 上,我仍然需要手动浏览到本地主机的地址才能使您的代码正常工作。

To browse to your locahost's address, type this in command line: sudo ifconfig

要浏览到您的 locahost 的地址,请在命令行中键入:sudo ifconfig

and note the inet address under eth0.

并记下 eth0 下的 inet 地址。

Otherwise, as others have noted, simply type node.js filename and it will work as long as you point the browser to the proper address.

否则,正如其他人所指出的,只需输入 node.js 文件名,只要您将浏览器指向正确的地址,它就会工作。

Hope this helps!

希望这可以帮助!