TypeError:object不是在express中显示的函数

时间:2015-03-23 11:34:45

标签: node.js

今天我正在学习Nodejs(初学者)并使用mysql执行CURD操作。我正在与http://teknosains.com/i/simple-crud-nodejs-mysql合作。一切正常,但是当我运行app.js时,我得到了这种类型的错误:

baltech@baltech121:/var/www/html/myapp$ nodejs app.js

/var/www/html/myapp/app.js:10
var app = express();

var app = express();
      ^
TypeError: object is not a function

at Object.<anonymous> (/var/www/html/myapp/app.js:10:11)
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

在这种情况下我该怎么办? 我的app.js就在这里

/**
 * Module dependencies.
 */
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
//load customers route
var customers = require('./routes/customers'); 
var app = express();
var connection  = require('express-myconnection'); 
var mysql = require('mysql');
// all environments
app.set('port', process.env.PORT || 4300);
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(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}
/*------------------------------------------
    connection peer, register as middleware
    type koneksi : single,pool and request 
-------------------------------------------*/
app.use(

    connection(mysql,{

        host: 'localhost',
        user: 'root',
        password : 'admin',
        port : 3306, //port mysql
        database:'nodejs'
    },'request')
);//route index, hello world
app.get('/', routes.index);//route customer list
app.get('/customers', customers.list);//route add customer, get n post
app.get('/customers/add', customers.add);
app.post('/customers/add', customers.save);//route delete customer
app.get('/customers/delete/:id', customers.delete_customer);//edit customer route , get n post
app.get('/customers/edit/:id', customers.edit); 
app.post('/customers/edit/:id',customers.save_edit);
app.use(app.router);
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

2 个答案:

答案 0 :(得分:12)

您的快递版本错误。您只能在v3.x.x中使用express()创建服务器。在此版本之前,express不能作为函数调用。

您可以使用

创建服务器
performSegueWithIdentifier

答案 1 :(得分:0)

问题可能是require语句的顺序 - require('http')应该是第一个。