Node.js node.exe不读取文件,但返回省略号(…)
2014年2月底,我将node下载到windows 7计算机的c:\dev\0.10\上,node.exe可以正常打开。 受Smashing Node(一些书)的启发,我希望实现以下目标: Firefox显示纯文本粉碎节点!如果我将其指向localhost:3000并在节点控制台中运行Node.js node.exe不读取文件,但返回省略号(…),node.js,Node.js,2014年2月底,我将node下载到windows 7计算机的c:\dev\0.10\上,node.exe可以正常打开。 受Smashing Node(一些书)的启发,我希望实现以下目标: Firefox显示纯文本粉碎节点!如果我将其指向localhost:3000并在节点控制台中运行 node my-web-server.js 其中node.exe旁边的my-web-server.js文件包含 require('http').createServer(function(req,res){r
node my-web-server.js
其中node.exe旁边的my-web-server.js文件包含
require('http').createServer(function(req,res){res.writeHead(200, ('Content-Type', 'text/html'));res.end('<marquee>Smashing Node!</marquee>');}).listen(3000);;
如果在节点中粘贴上面的一行,它将与
{ domain: null,
_events: ..etc... }
忽略这一点,浏览器F5将导致粉碎节点
Node拒绝最简单的文件,比如在Node.exe旁边有一个名为hello.js的文件,该文件包含ascii文本
console.log(“你好”);
i类型:
节点返回
...
...
(深灰色的省略号)
应为:节点返回hello
我键入一个不存在的文件,如下所示:
node die
节点返回
...
...
如果我打字
var http = require('http');
节点:
undefined
(暗灰色)预期的是:类似ok的东西,特别是因为上面的oneliner生成了一个web服务器
如果我怎么打字
npm install colors
节点与
npm should be run outside of the node repl, in your normal shell. (Press Control-D to exit.)
当然
node --version
也以省略号回应
我能做什么?
如何使节点处理文件?您需要在命令行shell(例如
cmd.com
或Windows PowerShell)上运行node hello.js
(和npm
)
您正在尝试在Node REPL(节点控制台)中运行它,在那里您只需要键入JavaScript。很难判断粉碎节点在哪里失败,特别是作为一个单行程序。代码在我的机器上运行正常,但您可以按如下方式拆分代码,并添加几个console.log()以查看执行情况:
console.log('about to start listening for requests');
require('http').createServer( function(req,res) {
console.log('a request was received', req.url);
res.writeHead(200, ('Content-Type', 'text/html'));
res.end('<marquee>Smashing Node!</marquee>');
}).listen(3000);;
console.log('listening for requests');
console.log('即将开始侦听请求');
require('http').createServer(函数(req,res){
log('收到请求',请求url);
res.writeHead(200,('Content-Type','text/html');
res.end('Smashing Node!');
}).听(3000);;
log(“侦听请求”);
像以前一样将此代码保存为my-web-server.js,然后从命令行“node my-web-server.js”运行,并将浏览器指向localhost:3000
另外,我还有一个非常基本的Node.js教程,可以帮助您掌握一些基本知识:aha。因此,在节点上工作时,不要使用节点shell,而是通过命令shell运行命令?不。只需运行脚本。没有必要运行REPL,除非您想逐行编写JS(这对测试东西很有用)。甚至比正确更好:我的路径上似乎有一个节点,可以在任何地方运行它。谢谢你救了我一天。我注意到node hello.js给出了“…”你的教程也没有提到必须从commmand提示符而不是node repl运行节点。哦,也许我是世界上唯一犯了那个错误的人。