Any suggestions to solve the following error based on the code presented below would be would be appreciated, as I'm simply lost here:
~/public_html/server/routes/api.js:10
api.use(function(req, res, next){
^
TypeError: Cannot call method 'use' of undefined
at Object.<anonymous> (~/public_html/server/routes/api.js:10:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (~/public_html/server/server.js:72:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
To start, here is the relevant structure of my app directory:
--public_html
--node_modules
--express
--pg
--body-parser
--server
--routes
api.js
index.js
server.js
I set up a basic express app in server.js for my routes:
// server.js
var express = require('../node_modules/express'),
app = express(),
bodyParser = require('../node_modules/body-parser'),
app.set('port', 3000);
app.use(bodyParser);
app.use('/api', require('./routes/api').api);
app.use('/', require('./routes/index').index);
// Start server
app.listen(app.get('port'), function () {
console.log("listening on port %d in %s mode", app.get('port'), app.settings.env);
});
Then, in my api.js, I create the routes and define my necessary HTTP verbs. I'll show 1 .get call for example here:
// api.js
var pg = require('../../node_modules/pg'),
express = require('../../node_modules/express'),
connString = "postgres://db_user:xxxx@localhost:5432/db_name";
var api = express.Router();
api.use(function(req, res, next){
console.log('in api.js...');
next();
});
api.get('getUsers', function(req, res) {
pg.connect(connString, function(err, client, done) {
if (err) {
console.error("Error fetching client from pool", err);
} else {
client.query('SELECT * FROM users',
function(err, result) {
done();
if (err) {
console.error("Error querying database", err);
} else {
res.json(result[0]);
}
});
}
});
});
module.exports.api = api;
The problem here is the way you're requiring your dependencies. Since they are node packages that you've installed in your project, you don't need to reference them by their file path. In fact, it can lead to problems if you reference the incorrect file name (when you reference a folder, it will automatically try to load index.js
from that folder).
So in your server.js
file, change this:
var express = require('../node_modules/express'),
app = express(),
bodyParser = require('../node_modules/body-parser');
To this:
var express = require('express'),
app = express(),
bodyParser = require('body-parser');
Also make similar changes in other places where you load dependencies which are node packages you have installed. (Please note: if you reference a local file which you've created within your project, you'll need to reference it by file location.)
For more information on how Node loads files via require, see the documentation: http://nodejs.org/api/modules.html#modules_all_together