Created
May 17, 2020 01:40
-
-
Save IPRIT/7741e71b8e5fc84a4029d4a8507ad5e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SSID = 'alexander'; | |
const PASSWORD = '12341234'; | |
const button = require('@amperka/button').connect(P7, { holdTime: 0.6 }); | |
const led = require('@amperka/led').connect(P6); | |
const memory = require('@amperka/card-reader').connect(P8); | |
const http = require('http'); | |
const wifi = require('@amperka/wifi').setup((err) => { | |
if (err) { | |
return console.log(err); | |
} | |
console.log('Connecting to Wi-Fi...'); | |
wifi.connect(SSID, PASSWORD, (err) => { | |
if (err) { | |
return console.log(err); | |
} | |
wifi.getIP((err, ip) => { | |
if (err) { | |
return console.log(err); | |
} | |
console.log('Connected to Wi-Fi! IP:', ip); | |
server.listen(3000); | |
}); | |
}); | |
}); | |
var Server = function() { | |
this._server = http.createServer(this._onPageRequest.bind(this)); | |
this._events = {}; | |
}; | |
Server.prototype.on = function(types, callback) { | |
if (typeof types == 'string') { | |
types = [types]; | |
} | |
for (var t in types) { | |
if (!this._events[types[t]]) { | |
this._events[types[t]] = callback; | |
} | |
} | |
}; | |
Server.prototype.listen = function(port) { | |
this._server.listen(port || 80); | |
}; | |
Server.prototype._onPageRequest = function(req, res) { | |
var request = url.parse(req.url, true); | |
this._event(request.pathname, req, res); | |
}; | |
Server.prototype._event = function(eventName, req, res) { | |
res.send = function(content, headers) { | |
if (headers === undefined) { | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
} else { | |
res.writeHead(200, headers); | |
} | |
res.write(content); | |
}; | |
if (this._events[eventName]) { | |
this._events[eventName](req, res); | |
} else { | |
res.writeHead(404, { 'Content-Type': 'text/html' }); | |
res.write('<h1>404 - Not found</h1>'); | |
} | |
res.end(); | |
}; | |
const server = new Server(); | |
server.on('/', (req, res) => { | |
const request = url.parse(req.url, true); | |
const params = request.query; | |
const path = decodeURIComponent(params.path || '') || '/'; | |
const dirs = memory.readDir(path); | |
let html = ''; | |
const pathParts = ['/'].concat(path.split('/')).filter(v => !!v); | |
let link = ''; | |
html += pathParts.map((part, index) => { | |
link += '/' + (part === '/' ? '' : part); | |
if (index === pathParts.length - 1) { | |
return '<h1 style="display: inline;">' + part + '</h1>'; | |
} | |
const newLink = '/?path=' + encodeURIComponent(link); | |
return '<h1 style="display: inline;"><a href="' + newLink + '">' + part + '</a></h1>' + ' / '; | |
}).join('') + '<br>'; | |
html += dirs.map(dir => { | |
const link = '/?path=' + encodeURIComponent((path === '/' ? '' : path) + '/' + dir); | |
return '<a href="' + link + '">' + dir + '</a><br>'; | |
}); | |
res.send(html); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment