简体   繁体   English

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

[英]How to run server written in js with Node.js

I have installed node.js from here http://nodejs.org/ .我已经从http://nodejs.org/安装了 node.js。 in my windows8 machine.在我的 windows8 机器上。 copied the example server code in my server.js file在我的 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.然后打开 node.js 提示并写入 node c:/node/server.js 但没有任何反应。 I am a php developer just trying hands on it, any guidelines will really be helpful.我是一名 php 开发人员,只是在尝试使用它,任何指导方针都会非常有帮助。

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

Nodejs is a scripting language (like Python or Ruby, and unlike PHP or C++). Nodejs 是一种脚本语言(类似于 Python 或 Ruby,与 PHP 或 C++ 不同)。 To run your code, you need to enter a command in the terminal / shell / command prompt.要运行您的代码,您需要在终端 / shell / 命令提示符中输入命令。 Look for an application shortcut in your operating system by one of those names.通过这些名称之一在您的操作系统中查找应用程序快捷方式。

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 .但是您首先需要在终端中浏览到与文件server.js相同的文件夹。 The syntax for using the terminal varies by operating system, look for its documentation.使用终端的语法因操作系统而异,请查找其文档。

I open a text editor, in my case I used Atom.我打开一个文本编辑器,在我的例子中我使用了 Atom。 Paste this code粘贴此代码

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.就在那里。

Just go on that directory of your JS file from cmd and write node jsFile.js or even node jsFile ;只需从cmd进入 JS 文件的那个目录并写入node jsFile.js甚至node jsFile both will work fine.两者都可以正常工作。

Just try试一试

node server

from cmd prompt in that directory从该目录中的 cmd 提示符

If you are in a Linux container, such as on a Chromebook, you will need to manually browse to your localhost's address.如果您在 Linux 容器中,例如在 Chromebook 上,您将需要手动浏览到您的本地主机地址。 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.我知道较新的 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!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM