Skip to content
Snippets Groups Projects
cf.js 2.84 KiB
Newer Older
  • Learn to ignore specific revisions
  • // new year new bootstrap
    
    const express = require('express')
    const app = express()
    
    Jake Read's avatar
    Jake Read committed
    // this include lets us read data out of put requests,
    const bodyparser = require('body-parser')
    
    Jake Read's avatar
    Jake Read committed
    // 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))
    
    Jake Read's avatar
    Jake Read committed
    app.use(bodyparser.json())
    app.use(bodyparser.urlencoded({extended: true}))
    
    // if these don't exist, they get 'nexted' to any other 'middleware' we write
    
    Jake Read's avatar
    Jake Read committed
    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,
    
    Jake Read's avatar
    Jake Read committed
    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)
    
    
    Jake Read's avatar
    Jake Read committed
    // want to announce our existence, this just logs our IPs to the console:
    
    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()
    */