npm install;
node local.js
Now, http://localhost:3000, and you should see got it.
node_modules |
npm install;
node local.js
Now, http://localhost:3000, and you should see got it.
'use strict'; | |
var joule = require('./joule'); | |
joule.get('/', function (event, context, res) { | |
res.send('Got it '); | |
}); | |
module.exports = joule; |
'use strict'; | |
var Router = require('i40'); | |
var Response = require('joule-node-response'); | |
var _ = require('lodash'); | |
var url = require('url'); | |
var JouleRouter = function () { | |
this.routes = {}; | |
}; | |
JouleRouter.prototype.addRouteForMethod = function (method, path, cb) { | |
method = method.toUpperCase(); | |
if (!this.routes[method]) { | |
this.routes[method] = new Router(); | |
} | |
this.routes[method].addRoute(path, cb); | |
}; | |
var initalMethods = ['get', 'post', 'put', 'delete']; | |
_(initalMethods).forEach(function(method) { | |
JouleRouter.prototype[method] = function (path, cb) { | |
return this.addRouteForMethod(method, path, cb); | |
}; | |
}); | |
JouleRouter.prototype.routePath = function (method, path) { | |
if (!this.routes[method]) { | |
return null; | |
} | |
var match = this.routes[method].match(path); | |
if (!match) { | |
return null; | |
} | |
return match.fn; | |
}; | |
var LocalResponse = function(res) { | |
this.response = res; | |
}; | |
LocalResponse.prototype.setContentType = function (contentType) { | |
this.response.setHeader('Content-Type', contentType); | |
}; | |
LocalResponse.prototype.setContext = function (){}; | |
LocalResponse.prototype.setHttpStatusCode = function(code) { | |
var reason; | |
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html | |
switch(code) { | |
case 200: | |
reason = 'OK'; | |
break; | |
case 201: | |
reason = 'Created'; | |
break; | |
case 202: | |
reason = 'Accepted'; | |
break; | |
case 400: | |
reason = 'Bad Request'; | |
break; | |
case 401: | |
reason = 'Unauthorized'; | |
break; | |
case 403: | |
reason = 'Forbidden'; | |
break; | |
case 404: | |
reason = 'Not Found'; | |
break; | |
case 409: | |
reason = 'Conflict'; | |
break; | |
default: | |
reason = 'Unknown'; | |
break; | |
} | |
this.response.statusCode = code; | |
this.response.statusMessage = reason; | |
}; | |
LocalResponse.prototype.send = function(data) { | |
var contentType = this.response.getHeader('content-type'); | |
if(typeof(this.response.statusCode) === 'undefined') { | |
this.setHttpStatusCode(200); | |
} | |
if(typeof(contentType) === 'undefined') { | |
this.setContentType('application/json'); | |
} | |
this.response.end(data); | |
}; | |
JouleRouter.prototype.routeForLocal = function (req, res) { | |
var path = url.parse(req.url).pathname; | |
console.log(req.path, path); | |
var cb = this.routePath(req.method, path); | |
var response = new LocalResponse(res); | |
var event = {}; | |
var context = {}; | |
console.log(cb); | |
if (!cb) { | |
response.send(''); | |
return; | |
} | |
return cb(event, context, response); | |
}; | |
module.exports = new JouleRouter(); |
'use strict'; | |
var connect = require('connect'); | |
var http = require('http'); | |
var app = connect(); | |
var jouleApp = require('./index'); | |
// respond to all requests | |
app.use(function(req, res){ | |
// res.end('Hello from Connect!\n'); | |
jouleApp.routeForLocal(req, res); | |
}); | |
//create node.js http server and listen on port | |
http.createServer(app).listen(3000); |
{ | |
"name": "lamba-router", | |
"version": "1.0.0", | |
"description": "A tool to route lambda functions", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"connect": "^3.4.1", | |
"i40": "^1.3.1", | |
"joule-node-response": "0.0.8", | |
"lodash": "^4.7.0" | |
} | |
} |