From b42f4dee6a00113c3c9bbedecbcac9903af39080 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Sat, 24 Sep 2016 11:47:01 -0700 Subject: [PATCH 1/3] Add bodyParser calls for urlencoded and json request content-types --- function-run/nodejs/server.js | 15 +++++++++++---- function-run/nodejs/test/test.js | 9 ++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/function-run/nodejs/server.js b/function-run/nodejs/server.js index 059189f4..c2672128 100644 --- a/function-run/nodejs/server.js +++ b/function-run/nodejs/server.js @@ -8,9 +8,13 @@ const bodyParser = require('body-parser'); // Command line opts const argv = require('minimist')(process.argv.slice(1)); -if (!argv.codepath || !argv.port) { - console.error("Need --codepath and --port"); - process.exit(1); +if (!argv.codepath) { + console.log("Codepath defaulting to /user.js"); + argv.codepath = "/user.js"; +} +if (!argv.port) { + console.log("Port defaulting to 8888"); + argv.port = 8888; } // User function. Starts out undefined. @@ -44,7 +48,10 @@ function specialize(req, res) { } +app.use(bodyParser.urlencoded()); app.use(bodyParser.json()); +app.use(bodyParser.raw()); + app.post('/specialize', specialize); // Generic route -- all http requests go to the user function. @@ -56,7 +63,7 @@ app.all('/', function (req, res) { const context = { request: req, response: res - // TODO: context should also have: URL template params, query string, ...anything else? + // TODO: context should also have: URL template params, query string }; function callback(status, body, headers) { if (!status) diff --git a/function-run/nodejs/test/test.js b/function-run/nodejs/test/test.js index 288ef627..906a5c84 100644 --- a/function-run/nodejs/test/test.js +++ b/function-run/nodejs/test/test.js @@ -1,5 +1,8 @@ module.exports = function (context, callback) { - console.log("Test function entered"); - callback(200, "Hello, world!\n"); - console.log("Test function exit"); + console.log("headers=", JSON.stringify(context.request.headers)); + console.log("body=", JSON.stringify(context.request.body)); + + //console.log(`Test function entered: ${context.request.body}`); + + callback(200, "Hello, world !\n"); } From 35e3b3b421074aa572a9f4bc53393dd3060b3ba2 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Sat, 24 Sep 2016 12:14:27 -0700 Subject: [PATCH 2/3] Add logging middleware to log requests --- function-run/nodejs/package.json | 3 ++- function-run/nodejs/server.js | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/function-run/nodejs/package.json b/function-run/nodejs/package.json index fcd3f671..fb4ae26f 100644 --- a/function-run/nodejs/package.json +++ b/function-run/nodejs/package.json @@ -15,6 +15,7 @@ "dependencies": { "express": "", "minimist": "", - "body-parser": "" + "body-parser": "", + "morgan": "" } } diff --git a/function-run/nodejs/server.js b/function-run/nodejs/server.js index c2672128..ec426c69 100644 --- a/function-run/nodejs/server.js +++ b/function-run/nodejs/server.js @@ -5,6 +5,7 @@ const process = require('process'); const express = require('express'); const app = express(); const bodyParser = require('body-parser'); +const morgan = require('morgan'); // Command line opts const argv = require('minimist')(process.argv.slice(1)); @@ -48,7 +49,10 @@ function specialize(req, res) { } -app.use(bodyParser.urlencoded()); +// Request logger +app.use(morgan('combined')) + +app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(bodyParser.raw()); From 5d76ee8894b0879931b927155914218be5b9dcf1 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Sat, 24 Sep 2016 12:17:08 -0700 Subject: [PATCH 3/3] Remove outdated comment --- function-run/nodejs/test/test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/function-run/nodejs/test/test.js b/function-run/nodejs/test/test.js index 906a5c84..6b6ae4fd 100644 --- a/function-run/nodejs/test/test.js +++ b/function-run/nodejs/test/test.js @@ -2,7 +2,5 @@ module.exports = function (context, callback) { console.log("headers=", JSON.stringify(context.request.headers)); console.log("body=", JSON.stringify(context.request.body)); - //console.log(`Test function entered: ${context.request.body}`); - callback(200, "Hello, world !\n"); }