Skip to content

Instantly share code, notes, and snippets.

View harrietty's full-sized avatar

Harriet harrietty

View GitHub Profile
.then(() => {
if (newJobs.length) {
var nexmo = new Nexmo({
apiKey: NEXMO_API_KEY,
apiSecret: NEXMO_API_SECRET
});
nexmo.message.sendSms('Donkey Jobs Finder', MY_PHONE_NUMBER, 'Hello, we found a new donkey job!');
}
callback(null, { jobs: newJobs });
})
function formatJobs (list) {
return list.reduce((acc, job) => {
return `${acc}${job.job} in ${job.location} closing on ${moment(job.closing).format('LL')}\n\n`;
}, 'We found:\n\n');
}
module.exports = {
extractListingsFromHTML,
formatJobs
};
const app = express();
const PORT = 3000;
app.listen(PORT, (err) => {
if (err) console.log(err);
else console.log(`App listening on port ${PORT}`)
});
const http = require('http');
function hexpress () {
const nodeServer = http.createServer((req, res) => {
// respond?
});
const app = {
listen: (...args) => {
nodeServer.listen(...args);
return nodeServer;
const http = require('http');
const url = require('url');
function Hexpress () {
}
Hexpress.prototype.listen = function (...args) {
const nodeServer = http.createServer((req, res) => {
const {pathname} = url.parse(req.url);
function addResMethods(res) {
res.status = function (code) {
this.statusCode = code;
return this;
};
res.sendStatus = function (code) {
this.statusCode = code;
this.write(getAppropriateTextResponse(code));
this.end();
Hexpress.prototype.listen = function (...args) {
const nodeServer = http.createServer((req, res) => {
res = addResMethods(res);
// now we have more methods to use!
res.status(200).send('Can I tempt you with a half price cauldron?');
});
nodeServer.listen(...args);
return nodeServer;
const app = express();
app.get('/api', (req, res) => res.send('All good'));
app.get('/api/hexes', (req, res) => res.send('So many hexes...'));
app.get('/api/hexes/words', (req, res) => res.send('List of words for hexes'));
app.delete('/api/hexes/:id', (req, res) => res.send(`Hex Number ${req.params.id} successfully deleted`));
function Hexpress () {
this.routeMappings = {};
};
Hexpress.prototype.get = function (route, handler) {
this.routeMappings[`GET ${route}`] = handler;
};
// Usage:
Hexpress.prototype.listen = function (...args) {
const nodeServer = http.createServer(function (req, res) {
res = addResMethods(res);
// What handler should I choose from our object?
const reqKey = `${req.method} ${req.url}`;
if (this.routeMappings[reqKey]) return this.routeMappings[reqKey](req, res);
else defaultResponse(req, res)