2

Today I am learning Nodejs (Beginner) and execute CURD operation with mysql. I am working with http://teknosains.com/i/simple-crud-nodejs-mysql . Everything working fine but as last when I am running app.js, I am getting this type of error:

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

What do I do in this case? My app.js is here

/**
 * 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'));
});
9
  • It seems that you don't have express installed in your project. Just run a npm install express. Commented Mar 23, 2015 at 11:42
  • @RodrigoMedeiros You can view my directory structure /app.js /nbproject /node_modules package.json /public /routes /views express is already install Commented Mar 23, 2015 at 11:46
  • What is your express version? What's the result of running npm ls express from your project root folder? Commented Mar 23, 2015 at 11:59
  • @RodrigoMedeiros When i am running above command on root getting [email protected] but in project folder this one is different have [email protected] Commented Mar 23, 2015 at 12:02
  • That's it: the express version on your project is < 3.x.x. Before 3.x.x, your express app should've been instantiated with express.createServer(). From 3.x.x to now, you just have to do express() to get the same result. So, just update the express version on your package.json and run npm install in your project root folder again. Commented Mar 23, 2015 at 12:11

2 Answers 2

12

You have the wrong express version. You can only create the server with express() in v3.x.x. Before this version, express can not be called as a Function.

You can create server using

var app = express.createServer();
Sign up to request clarification or add additional context in comments.

1 Comment

Some example with explanation would be great.
0

The problem may be in the order of the require statements - require('http') should come first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.