#!/usr/bin/env node var async = require('async'); var UrlResolver = require('urlresolver'); var p = new UrlResolver(); var user = 'me'; exports.p = p; var todo=require('./todo'); var {pulltasks,addtask,removetask,ranktasks,removetasks}=todo; Object.keys(todo).forEach(k=>exports[k]=todo[k]); exports.de=de; function de(res,err,code) { res.writeHead(code||400,{"Content-Type":'text/plain'}); res.end(err.toString()); } exports.simpcb=simpcb; function simpcb(res) { return (err,data)=>{ if(err) return de(res,err); if(data) return res.end(data.toString()); res.end('success'); }; } p.add('/pull',(req,res)=>{ pulltasks(user,5,(err,tasks)=>{ if(err) return cb(err); res.writeHead(200,{"Content-Type":'text/plain'}); res.end(tasks.map((i,ind)=>`${i.id}: ${ind} ${i.title}`).join('\n')); }); }); p.add('/api/pull',(req,res)=>{ //Replace with req.user pulltasks(user,5,(err,tasks)=>{ if(err) return cb(err); res.writeHead(200,{"Content-Type":'application/json'}); res.end(JSON.stringify(tasks)); }); }); p.add('/add/task/',(req,res)=>{ addtask(req.user,req.post.string,simpcb(res)); }); p.add('/remove/task',(req,res)=>{ var taskid=parseInt(req.post.string); if(isNaN(taskid)) return de(res,"Not a number"); removetask(req.user,taskid,simpcb(res)); }); p.add('/rank/tasks',(req,res)=>{ var tasks=req.post.string.split(',').map(i=>parseInt(i)).filter(i=>!isNaN(i)); ranktasks(req.user,tasks,simpcb(res)); }); p.add('/rank/and/remove/tasks',(req,res)=>{ var [rank,remove]=req.post.string.split('|').map(list=>list.split(',').map(i=>parseInt(i)).filter(i=>!isNaN(i))); async.parallel([ cb=>ranktasks(req.user,rank,cb), cb=>removetasks(req.user,remove,cb) ],simpcb(res)); }); function exports(req,res) { p.get(req.url)(req,res); } require('http').createServer(exports).listen(8092);