//emitter.js
const {EventEmitter} = require('events');
module.exports = new EventEmitter();
//module.js
const e = require('./emitter.js');
e.on('test', n => console.log(n));
//main.js
const e = require('./emitter.js');
require('./module.js');
setInterval(() => { e.emit('test', Math.round(Math.random() * 10)) }, 1000);
#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony --harmony_destructuring "$0" "$@"
$ node -e "require('dns').reverse('178.66.32.125', (err, hosts)=>console.log(hosts))"
npm start [-- <args>]
This runs an arbitrary command specified in the package's "start" property of its "scripts" object. If no "start" property is specified on the "scripts" object, it will run node server.js.
14 error enoent ENOENT, open 'C:\Users\Sergey\package.json'
{
"scripts": {
"start": "node server.js"
}
}
app.use(function* (next) {
if (this.request.path === '/') {
this.response.body = 'hello world';
} else {
//yield next;
}
});
app.use(function* (next) {
this.response.body = 'hello world not from root';
yield next;
});
var gulp = require('gulp');
var spawn = require('child_process').spawn;
var ping;
gulp.task('ping', function() {
ping = spawn('ping', ['localhost'], {
stdio: 'inherit' // для примера, серверу не нужно
});
});
gulp.task('test', ['ping'], function() {
setTimeout(function() {
ping.kill();
}, 2000);
});
app.io = function(server) {
var io = require('socket.io')(server);
io.on('connection', function (socket) {
//...
});
}
app.io(server);
// routes/socket.js
function handler(socket) {
// ...
}
module.exports = handler;
// app.js
var io = require('socket.io');
var socket = require('./routes/socket');
app.io = function(server) {
io(server).on('connection', socket);
};
// bin/www
app.io(server);
nickForm.submit(function(){
...
return false;
});
<input type="text" class="nickname">
не совпадает с nickInput = $('.nick')
var fs = require('fs');
var fd = fs.openSync('./text', 'r');
var decoder = new (require('string_decoder').StringDecoder)();
var buf = new Buffer(10);
var list = [], str, bytesReaded;
while (bytesReaded = fs.readSync(fd, buf, 0, 10, null)) {
str = (list.pop() || '') + decoder.write(buf.slice(0, bytesReaded));
list = list.concat(str.split("\n"));
}
console.log(list);
var fs = require('fs');
var list = [];
fs.createReadStream('./text', {encoding: 'utf8'})
.on('readable', function() {
var str, chunk;
while (chunk = this.read(10)) {
str = ((list.pop() ||'') + chunk);
list = list.concat(str.split('\n'));
}
list[list.length-1] += (this.read() || '');
})
.on('end',function() {
console.log(list);
});