Newer
Older
// new year new bootstrap
const express = require('express')
const app = express()
// this include lets us read data out of put requests,
const bodyparser = require('body-parser')
// our fs tools,
const filesys = require('./filesys.js')
// will use these to figure where tf we are
const os = require('os')
let ifaces = os.networkInterfaces()
// serve everything: https://expressjs.com/en/resources/middleware/serve-static.html
app.use(express.static(__dirname))
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({extended: true}))
// if these don't exist, they get 'nexted' to any other 'middleware' we write
app.get('/fileList', (req, res) => {
try{
// we would fs/ through our list, and serve that,
filesys.getDirTree(req.query.path).then((list) => {
// take the query out of the front of the path, and swap all \ for /
for(i in list){
list[i] = list[i].substring(req.query.path.length + 1)
let decomp = list[i].split('\\')
let np = ''
for(l in decomp){
np += `/${decomp[l]}`
}
// wash of first / items and of file extension,
list[i] = np.substring(2, np.indexOf('.'))
}
res.send(list)
// ship up north,
}).catch((err) => {
res.send(err)
// ship err back
})
} catch (err) {
console.log(err)
res.send('server-side error retrieving list')
}
})
// we also handle file-saving this way,
app.post('/save/contexts/:context/:file', (req, res) => {
// this is probably fine for now, but I kind of want a websocket to do this kind of stuff ?
console.log('put req to context' + req.params.context, 'with name', req.params.file)
console.log('the file as', req.body)
// now we fs.write() this thing, and reply...
res.send({ok: true})
})
let port = 8080
// and listen,
app.listen(port)
// want to announce our existence, this just logs our IPs to the console:
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Object.keys(ifaces).forEach(function(ifname) {
var alias = 0;
ifaces[ifname].forEach(function(iface) {
if ('IPv4' !== iface.family){//} || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
console.log('cf available on: \t' /*ifname + ':' + alias,*/ + iface.address + `:${port}`);
// this single interface has multiple ipv4 addresses
// console.log('serving at: ' ifname + ':' + alias + iface.address + `:${port}`);
} else {
console.log('cf available on:\t' /*ifname + ':' + alias,*/ + iface.address + `:${port}`);
// this interface has only one ipv4 adress
//console.log(ifname, iface.address);
}
++alias;
});
});
/*
let begin = () => {
// setup to dish files,
ex.get('/', (req, res) => {
console.log('req /')
res.sendFile(__dirname + '/index.html')
})
http.listen(8080, () => {
console.log("is listening on 8080")
})
}
begin()
*/