将对象传递给护照

时间:2015-04-23 03:09:10

标签: javascript node.js express typeerror

我得到typeError:object不是函数

我跑的时候:

node server.js

以下代码行

require('./app/routes.js')(app, passport);

来自我的server.js文件的以下代码:

// server.js

// set up ======================================================================
// get all the tools we need
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');

var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');

var configDB = require('./config/database.js');

// configuration ================================================================
mongoose.connect(configDB.url); // connect to our database

// require('./config/passport')(passport); // pass passport for configuration


// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json()); // get info from html forms
    app.use(bodyParser.urlencoded({ extended: true}));

app.set('view engine', 'ejs'); // set up ejs for templating

// requirements for passport:
app.use(session({ secret: 'ilovescotchscotchyscotchscotch'}));
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('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport

// launch =======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);

不完全确定为什么会发生这种情况......

我正在按照本教程btw:https://scotch.io/tutorials/easy-node-authentication-setup-and-local

谢谢!

1 个答案:

答案 0 :(得分:2)

根据您的评论:这个post可以很好地解释如何在nodeJS中运行(它不是Javascript的东西)。基本上,通常需要每个可能需要的文件导出需求文件可以分配给变量的内容(例如var someVar = require('someFile');)。 IF 导出的值是一个函数,然后您可以在分配给该变量之前立即调用它(例如var someVar = require('someFile')(params);)。对于您的错误,似乎文件./app/routes.js未导出函数。

文件:./ app / rroutes.js

// some code that does cool stuff
// ...
// Time to expose whatever we want to the requiring file
module.exports = someObject; // Or value, function, etc.

文件:main.js

// we want cool stuff from routes.js
var coolStuff = require('./app/routes.js');

// someObject from ./app/routes.js is now assigned to coolStuff
coolStuff(); // ERROR: object is not a function

console.log(typeof coolStuff) // "object"