nodejs/express - cannot load routes using passport - node.js

I am trying to use passport to add user sessions on my webapp. I have configure my main js file as follow:
/**
* Module dependencies.
*/
var express = require('express');
var user = require('./server/routes/user');
var routes = require('./server/routes/index');
var http = require('http');
var path = require('path');
var app = express();
var passport = require('passport');
var flash = require('connect-flash');
require('./server/config/passport')(passport); // pass passport for configuration
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.configure(function(){
// required for passport
app.use(express.session({ secret: 'ilovesefounders' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
})
// routes ======================================================================
require('./server/routes/index')(app, passport); // load our routes and pass in our app and fully configured passport
routes.mount(app);
However when i try to launch it I get the following error:
require('./server/routes/index')(app, passport); // load our routes and pass i
^
TypeError: object is not a function
at Object.<anonymous> (/Path/app.js:38:33)
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 Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
What's going wrong here?

I don't know how your index.js looks like but you probably did not export your function module.
Check if index.js looks something like this:
module.exports = function(app, passport) {
/* code */
};
And you don't need to require "./server/routes/index" again.

Related

What does this error mean and how do I fix it? Error: Cannot find module 'html'

I've been at this for hours and can't seem to figure out how to get it to work.
Here is the error I am getting when I run node dNode.js
Error: Cannot find module 'html'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/Users/ricky/LA/dNode.js:3:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
Here is what a portion of my code looks like
var express = require('express');
var mysql = require('mysql');
var html = require('html');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended: false}));
var handlebars = require('express-handlebars').create({defaultLayout: 'main'});
app.use(bodyParser.json());
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 3001);
app.use(express.static(__dirname + '/public'));
Try removing var html = require('html');or run
npm install html
First off, it is very hard to do try to solve your problem with so little given.
However, based on what you have given you need to specify your view folder and parse the engine to HTML.
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('*', function(req, res){
res.render('index.html');
});
app.listen(app.get('port'), function() {
});

express - app `index.jade` not properly supplied by the `exports` function

I am trying to make a app, in the app directory this is my app.js file. in the app.js I am requiring the router folder and exporting the Routes. But I am not able to render the index.jade using the Router instead i am getting error:
D:\Projects\socialApp\node_modules\express\lib\router\index.js:423
throw new TypeError(msg);
^
TypeError: Router.use() requires callback function but got a [object Undefined]
at D:\Projects\socialApp\node_modules\express\lib\router\index.js:423:13
at Array.forEach (native)
at Function.use (D:\Projects\socialApp\node_modules\express\lib\router\index
.js:419:13)
at D:\Projects\socialApp\node_modules\express\lib\application.js:178:21
at Array.forEach (native)
at Function.use (D:\Projects\socialApp\node_modules\express\lib\application.
js:175:7)
at Object.<anonymous> (D:\Projects\socialApp\app.js:22:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
D:\Projects\socialApp>
here is my app.js:
var express = require("express"),
passport = require("passport"),
passportLocal = require("passport-local"),
bodyParser = require("body-parser"),
cookieParser = require("cookie-parser"),
expressSession = require("express-session"),
jade = require("jade"),
mongoose = require("mongoose"),
routes = require('./routes'),
port = process.env.PORT || 3000,
app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
var routes = require('./routes/index')(passport); //i am calling here
app.use('/', routes); // not getting index.jade on screen.
app.listen(port, function(){
console.log('Port Listing to ' + port + ":" + __dirname);
});
here is what in the router folder and in the index.js:
var express = require('express'),
router = express.Router();
module.exports = function(passport){
router.get('/', function(req,res){
res.render('index'); //i am not getting index.jade from view folder getting error
});
}
any one figure-out the issue please?
You need to return router; at the end of your module.exports function.

First nodejs app, it is saying to update your less-middleware usage

I installed nodejs using homebrew just now.
I then created a folder, and installed express:
npm install -g express
I then created a new project using:
express -H --css less --sessions foo
Now when I try and run the app:
node app.js
I get this:
/Users/blah/dev/testing/nodetest1/foo/node_modules/less-middleware/lib/middleware.js:50
throw new Error('Please update your less-middleware usage: http://goo.gl/Y
^
Error: Please update your less-middleware usage: http://goo.gl/YnK8p0
at module.exports.less.middleware (/Users/blah/dev/testing/nodetest1/foo/node_modules/less-middleware/lib/middleware.js:50:11)
at Object.<anonymous> (/Users/snad/dev/testing/nodetest1/foo/app.js:26:35)
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 Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
My app.js looks like:
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('less-middleware')({ src: path.join(__dirname, 'public') }));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
I changed the line that was:
app.use(require('less-middleware')({ src: path.join(__dirname, 'public') }));
to:
app.use(require('less-middleware')(path.join(__dirname, 'public')));
and it works now. Strange how this was a brand new app with a fresh install of nodejs and express.
I commented out
app.use(require('less-middleware')({ src: path.join(__dirname, 'public') }));
entirely and achieved the same result. I feel that we are breaking functionality of some sort.

TypeError: Cannot read property 'databaseName' of undefined

I've been banging my head against the wall. I've been doing the lynda.com tutorail on essential Node.js and I'm stuck at this point.
When i type nodemon server.js I get the error. I'm having problems exporting the db connection in node.js. Any hints? Is there a better way to debug then make a change and refresh in Node?
/Users/khebert/Sites/node/airline/node_modules/connect-mongo/lib/connect-mongo.js:87
this.db = new mongo.Db(options.mongoose_connection.db.databaseName,
^
TypeError: Cannot read property 'databaseName' of undefined
at new MongoStore (/Users/khebert/Sites/node/airline/node_modules/connect-mongo/lib/connect-mongo.js:87:60)
at module.exports (/Users/khebert/Sites/node/airline/app.js:22:10)
at Object.<anonymous> (/Users/khebert/Sites/node/airline/server.js:4:27)
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 Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
2 Jan 08:04:56 - [nodemon] app crashed - waiting for file changes before starting...
db.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/flights', function(err) {
if (err) console.log(err);
else console.log('DB success');
});
module.exports = mongoose.connections;
server.js
var http = require('http'),
flights = require('./data'),
db = require('./db'),
app = require('./app.js')(flights, db);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
app.js
module.exports = function(flights, db) {
/**
* Module dependencies.
*/
var express = require('express');
var MongoStore = require('connect-mongo')(express);
var routes = require('./routes')(flights);
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.session({
secret: 'keyboard cat',
store: new MongoStore({
mongoose_connection: db
})
}));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(function (req, res, next) {
res.set('X-Powerd-By','Flight Tracker');
next();
});
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' === app.get('env')) {
app.use(express.errorHandler());
}
app.get('/flight/:number', routes.flight);
app.put('/flight/:number/arrived', routes.arrived);
app.get('/list', routes.list);
app.get('/arrivals', routes.arrivals);
return app;
};
It might be a problem with the 'express.bodyParser()', you have to define your connections AFTER all the configuration of your app.
In db.js,
module.exports = mongoose.connections;
change to:
module.exports = mongoose.connections[0];
You can just reuse the db object already instantiated by mongoose!
new MongoStore({
db: mongoose.connection.db
})

Why is express telling me that my default view engine is not defined?

I'm using nodejs and mongodb in the back end for an app I'm working on. I'm using express to test the app, and I'm trying to use ejs to render my html files. However, I'm having the issue of my default view engine not being defined.
Here is my app.js:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var conf = require('./conf');
var app = express();
var mongoose = require('mongoose');
, Schema = mongoose.Schema
, ObjectId = mongooseSchemaTypes.ObjectID;
var UserSchema = new Schema({})
, User;
// all environments
app.set('port', process.env.PORT || 3000);
app.set('view engine', 'ejs');
app.engine('.html', require('ejs').renderFile());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Here is my package.json:
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.3.3",
"ejs":">= 0.0.1",
"mongoose-auth": ">= 0.0.12",
"mongoose": ">=2.4.8",
"everyauth": ">=0.2.28"
}
}
ERRORS:
Express
500 Error: Failed to lookup view "index"
at Function.app.render (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:494:17)
at ServerResponse.res.render (/home/christian/node_modules/nave/create/node_modules/express/lib/response.js:756:7)
at exports.index (/home/christian/node_modules/nave/create/routes/index.js:7:7)
at callbacks (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:161:37)
at param (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:135:11)
at pass (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:170:5)
at Object.router (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:33:10)
at next (/home/christian/node_modules/nave/create/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.methodOverride [as handle] (/home/christian/node_modules/nave/create/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:49:5)
Now when I try to run it my terminal outputs:
/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:173
if ('function' != typeof fn) throw new Error('callback function required');
^
Error: callback function required
at Function.app.engine (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:173:38)
at Function.<anonymous> (/home/christian/node_modules/nave/create/app.js:26:9)
at Function.app.configure (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:392:61)
at Object.<anonymous> (/home/christian/node_modules/nave/create/app.js:23: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 Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Any help would be greatly appreciated.
The source of the error describes the requirements:
if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');
Express expects that you either specify the view with its extension:
res.render('index.html');
Or specify a default view engine and name your views after it:
app.set('view engine', 'ejs');
// `res.render('index')` renders `index.ejs`
Regarding your edit:
if ('function' != typeof fn) throw new Error('callback function required');
The issue is with this line:
app.engine('.html', require('ejs').renderFile());
As the documentation demonstrates, app.engine() is expecting a function reference. You can do this by simply removing the () that call renderFile:
app.engine('.html', require('ejs').renderFile);
need to do all the app.set and app.use in an app.configure
try this
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('view engine', 'ejs');
app.engine('.html', require('ejs').renderFile());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
});

Resources