on your server run:
node tail.js file1 file2
in your browser open
http://localhost:8090/files/file1
and voila all changes show up in your client/browser
.idea | |
node_modules | |
atlassian-ide-plugin.xml |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
"http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<title></title> | |
<script type="text/javascript" src="http://yui.yahooapis.com/3.6.0/build/yui/yui-min.js"></script> | |
<script type="text/javascript" src="/socket.io/socket.io.js"></script> | |
</head> | |
<body> | |
<div id='files'></div> | |
<pre id="output"></pre> | |
<script type="text/javascript"> | |
YUI().use('node', function(Y) { | |
var path = window.location.pathname, | |
output = Y.one('#output'), | |
files = Y.one('#files'), | |
socket= io.connect(path) | |
; | |
socket.on('files', function (data) { | |
Y.log(data); | |
output.append(data); | |
Y.Array.each(data, function (file, index) { | |
files.append('<a href="/files/'+index+'">'+file+'</a> '); | |
}); | |
}); | |
socket.on('message', function(data) { | |
output.append(data); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
{ | |
"name": "tailjs", | |
"description": "broadcast logs using websockets", | |
"version": "0.0.3", | |
"private": true, | |
"dependencies": { | |
"socket.io": "*", | |
"socket.io-client": "*", | |
"express": "*" | |
}, | |
"homepage": "https://gist.github.com/3447400", | |
"repository": "git://gist.github.com/3447400.git", | |
"author": ["Maciej Kowalski"], | |
"keywords": [ "tail", "websocket", "express" ], | |
"bin": "./tail.js", | |
"config": { | |
"port": 8090 | |
} | |
} |
#!/usr/bin/env node | |
var fs = require('fs'), | |
port = process.env.npm_package_config_port || 8090, | |
express = require('express'), | |
http = require('http'), | |
app = express(), | |
server = http.createServer(app), | |
io = require('socket.io').listen(server), | |
files = [], | |
os = require('os'), | |
buf = new Buffer(4096); | |
server.listen(port); | |
if (process.argv.length < 3) { | |
console.error("Usage: " + __filename + " filename"); | |
process.exit(1); | |
} | |
// server the browser dependencies | |
app.configure(function () { | |
app.use(express.static(__dirname + '/public')); | |
}); | |
// server socket.io client to the browser | |
app.get('/socket.io/socket.io.js', function (req, res) { | |
res.sendfile(__dirname + '/node_modules/socket.io-client/dist/socket.io.js'); | |
}); | |
// open a route for each file from the command line | |
app.get('/files/:filename', function (req, res) { | |
res.sendfile(__dirname + '/public/index.html'); | |
}); | |
// for each file from the command line open an route | |
process.argv.splice(2).forEach(function (filename) { | |
"use strict"; | |
fs.open(filename, 'r', function (err, fd) { | |
if (err) { | |
console.error('Unable to open: ' + filename); | |
return; | |
} | |
files.push(filename); | |
var nsName = '/files/' + (-1 + files.length), | |
ns = io.of(nsName) | |
.on('connection', function(socket) { | |
socket.emit('files', files); | |
}); | |
console.info('Listening on http://' + os.hostname() + ':' + port + nsName + ' ' + filename); | |
// watch file | |
fs.watchFile(filename, function (curr, prev) { | |
var len = curr.size - prev.size, position = prev.size; | |
if (len > 0) { | |
fs.read(fd, buf, 0, len, position, | |
function (err, bytesRead, buffer) { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
var msg = buffer.toString('utf8', 0, bytesRead); | |
ns.emit('message', msg); | |
}); | |
} else { | |
console.log(curr); | |
} | |
}); | |
}); | |
}); | |
console.info('Listing on http://' + os.hostname() + ':' + port); | |
// on connection broadcast file names the server is tailing | |
io.sockets | |
.on('connection', function (socket) { | |
"use strict"; | |
// broadcast opened files | |
socket.emit('files', files); | |
}); |
Hello
how can i fix this error below?
io = require('socket.io').listen(server),
^
TypeError: require(...).listen is not a function
at Object. (/data/data/com.termux/files/home/andronode/web2/tail.js:8:31)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
at Module.load (node:internal/modules/cjs/loader:998:32)
at Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
Node.js v18.7.0
on your server run:
node tail.js path_to_log_you_wish_to_track
in your browser open
client.html?host=hostname
by the default hostname is localhostand voila all changes show up in your client/browser