bodyParser not found

Not applicable

NOTE: Running all code at Apigee. No local Apigee at all.

The code

'use strict';var express =
require('express');var bodyParser =
require('body-parser');var cookieParser =
require('cookie-parser');module.exports =
function(app) { 
app.use(express.static('public', {  dotfiles:
'ignore', 
etag: false, 
extensions: ['js', 'css', 'less'], 
index: false,  })); 
app.set('views', 'views');  app.set('view
engine', 'jade'); 
app.use(bodyParser.urlencoded({extended: false})); 
app.use(bodyParser.json()); 
app.use(cookieParser());};

gives the error

{"fault":{"faultstring":"Script node executed prematurely: 
Error: Cannot find module 'body-parser'\n
Error: Cannot find module 'body-parser'\n  
at module.js:340\n  
at module.js:280\n  
at module.js:364\n  
at require (module.js:380)\n  
at \/organization\/environment\/api\/config.js:4\n  
at module.js:456\n  
at module.js:474\n  
at module.js:356\n  
at module.js:312\n  
at module.js:364\n  
at require (module.js:380)\n  
at \/organization\/environment\/api\/hello-world.js:7\n 
at module.js:456\n  
at module.js:474\n  
at module.js:356\n  
at module.js:312\n  
at module.js:497\n  
at startup (trireme.js:142)\n 
at trireme.js:923\n","detail":{"errorcode":"scripts.node.runtime.ScriptExitedError"}}}

Any idea what I need to look for?

I am new to node at Apigee

Solved Solved
1 10 1,853
1 ACCEPTED SOLUTION

Not applicable

Instead of relying on running npm on Edge, I recommend:

1. Installing your module dependencies locally (preferably using package.json for npm)

2. Use "apigeetool deploynodeapp" with the -U option. This option will upload the dependencies.

See: https://www.npmjs.com/package/apigeetool

Scott

View solution in original post

10 REPLIES 10

@mplungjan@epo.org AFAIK Express module no longer supports body-parser hence it needs to be installed and used separately.

Kindly follow the below URL,

https://www.npmjs.com/package/body-parser

Ahh - I see.

In the article here https://community.apigee.com/articles/2332/how-to-...

I saw it was using the bodyParser but failed to notice it was npm installed before it was used.

Do you know how to access/load the bodyParser at Apigee Edge? I assume npm can only be run on local installations.

Thanks

I think in Apigee we can use body-parser as following,

var express = require('express');

var app = express();
app.use(express.bodyParser());

Give it a try.

body-parser was removed from the Express Connect middleware with Connect 3.0. Indeed you can install it separately. To deploy custom NPM modules to Edge, you can use apigeetool - also available on NPM: https://www.npmjs.com/package/apigeetool

adas
New Member

You can also use the npm management apis in edge as long as you have defined the dependencies in your package.json. The apigee docs should be self sufficient on how to use the npm apis.

Er, well, yes - they look complete. Quite a steep learning curve though.

Not applicable

Instead of relying on running npm on Edge, I recommend:

1. Installing your module dependencies locally (preferably using package.json for npm)

2. Use "apigeetool deploynodeapp" with the -U option. This option will upload the dependencies.

See: https://www.npmjs.com/package/apigeetool

Scott

This ALMOST worked. We have the following which does not work since it wants the "folder" of our app in the path

module.exports = function(app) { 
  app.post('/xxx/yyy', yyy); 
  app.get('/xxx/zzz', zzz); 
};

But the above maps to

http://ourdomain.apigee.com/xxx

instead of

http://ourdomain.apigee.com/ourproxyname/xxx

So we need something like an environment var or is there a better way?

module.exports = function(app) { 
  var root = process.env.APIGEE_PROXY; 
  app.post(root + '/xxx/yyy', yyy); 
  app.get(root + '/xxx/zzz', zzz); 
};

You can specify the base path for your proxy using apigeetool:

--base-path -b

That will tell the system to place the proxy at that path. So if you specified "-b /", that would match your path from the root.

Thanks for the reaction . I believe we did

--base-path -b ourproxyname

which means everywhere EXCEPT in the example I gave, we use relative path, but in my example, relative path does not work.

I have to ask my colleague who had the keyboard what he set is the base-path