我#!/usr/bin/env node在一些例子的开头看到了这一行,nodejs我用google搜索没有找到任何可以回答该行原因的话题.
单词的性质使搜索变得不那么容易.
我最近读了一些javascript和nodejs书,我不记得在其中任何一个看过它.
如果你想要一个例子,你可以看到RabbitMQ官方教程,他们几乎在所有的例子中都有它,这里有一个:
#!/usr/bin/env node
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var ex = 'logs';
var msg = process.argv.slice(2).join(' ') || 'Hello World!';
ch.assertExchange(ex, 'fanout', {durable: false});
ch.publish(ex, '', new Buffer(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function() { conn.close(); process.exit(0) }, 500);
});
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这条线的含义是什么?
如果我放入或删除此行有什么区别?在什么情况下我需要它?
我正在尝试获取有关在 NodeJs 中使用EventSource时出现的错误的信息,我想您可以通过以下示例更好地理解我:
var url = 'http://api.example.com/resource'
var EventSource = require('eventsource');
var es = new EventSource(url);
es.onmessage = function(e) {
console.log(e.data);
};
es.onerror = function(event) {
console.log(event);
};
Run Code Online (Sandbox Code Playgroud)
在onerror函数中,我想获取有关错误的信息,但该错误以及对象event是空的或未定义的es(嗯,这个对象只是带有一对大括号{})。我想在出现错误时读取响应标头,例如:
es.onerror = function(e) {
console.log(e.header.location);
};
Run Code Online (Sandbox Code Playgroud)
这可能吗?我缺少什么?我认为答案应该很简单,但我是 NodeJs 的新人。
所以基本上我有一个函数,仅当参数等于某个值时我才想对其行为进行存根。例子
var sinon = require('sinon');
var foo = {
bar: function(arg1){
return true;
}
};
var barStub = sinon.stub(foo, "bar");
barStub.withArgs("test").returns("Hi");
// Expectations
console.log(foo.bar("test")); //works great as it logs "Hi"
// my expectation is to call the original function in all cases except
// when the arg is "test"
console.log(foo.bar("woo")); //doesnt work as it logs undefined
Run Code Online (Sandbox Code Playgroud)