Last active
September 4, 2017 22:18
-
-
Save tomfa/6500ed1e1c4076b5e6ec7c534b7c52ba to your computer and use it in GitHub Desktop.
Small script that serves static files in hubot
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
// index.html up at localhost:8080/status | |
// | |
// requirements: mime (npm install mime --save) | |
const path = require('path'); | |
const fs = require('fs'); | |
var mime = require('mime'); | |
const serveFile = filePath => (req, res) => { | |
const fullPath = path.join(__dirname, filePath); | |
fs.readFile(fullPath, {encoding: 'utf-8'}, function(err, data){ | |
if (!err) { | |
res.writeHead(200, {'Content-Type': mime.lookup(fullPath)}); | |
res.write(data); | |
res.end(); | |
} else { | |
console.log(err); | |
} | |
}); | |
}; | |
function hubotFunctions(robot) { | |
robot.router.get('/status', serveFile('../public/index.html')); | |
} | |
module.exports = hubotFunctions; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment