TypeError:对象不是在 express 中显示的函数
2015-03-23
9719
今天我正在学习 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个回答
您使用的 express 版本不对。您只能在 v3.x.x 中使用 express() 创建服务器。在此版本之前,express 不能作为函数调用。
您可以使用
var app = express.createServer();
创建服务器
Vishal Rajole
2015-07-11
问题可能出在 require 语句的顺序上 - require('http') 应该放在第一位。
Stephen W. Wright
2018-02-13