对象预计微软的Jscript运行时错误 - 节点JS
我学习节点JS和我得到预期的对象,在1号线的Microsoft JScript运行时错误运行代码时对象预计微软的Jscript运行时错误 - 节点JS
var fs = require('fs');
function FileObject() {
this.filename = null;
this.exists = function(callback) {
var self = this;
fs.open(this.filename, 'r', function(err, handle){
if(err){
console.log(self.filename+ 'does Not exist');
callback(false);
}
else {
console.log(self.filename+ 'does Exist Indeed');
callback(true);
fs.close(handle);
}
});
};
}
var fo = new FileObject();
fo.filename = 'doesnotexist';
fo.exists(function(does_it_exist) {
console.log('results from exists:' + does_it_exist);
});
不能双击源文件来运行它,您需要从命令行执行脚本(例如C:\> node foo\bar.js
,假设您的脚本bar.js
在C:\foo
中)。
即使从命令提示符执行代码,我收到此错误。 – SankarSV4791 2014-10-05 16:46:00
请不要复制和重复已由某人回答的答案。 – 2017-09-21 14:07:19
@PushpSingh你指的是什么?我的回答并非从任何地方*复制和粘贴。此外,这个答案是在本文写作之前提交的唯一一个答案之前的7个月*,所以它甚至不是该答案的“副本”。 – mscdex 2017-09-21 16:41:07
node.js
文件名在NodeJS中保留。我也得到了同样的错误。我重新命名文件为 “test.js” 和它的工作...
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World OKKK");
response.end();
}).listen(8888);
命令提示符>node test.js
URL =>http://localhost:8888和它打印的 “Hello World OKKKK” 在浏览器上。
我所有的网络给你先生 – m02ph3u5 2015-11-27 15:30:38
*您如何运行此脚本? – apsillers 2014-09-30 19:47:19
我试图从命令提示符运行代码 – SankarSV4791 2014-10-05 16:44:33
您能准确地显示您是如何尝试从命令行执行脚本的吗? – mscdex 2014-10-05 17:29:30