如何使用带有mysql的node.js表达来执行crud

时间:2014-11-07 06:19:02

标签: mysql node.js express

我想用mysql执行一个基本的CRUD,我安装了一些模块,比如npm install mysql,npm install path,npm install routes但是我遇到的问题是{{1}这里的错误是我的

app.js

middleware

这是其他customer.js

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 || 80);
    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());
    }

    app.use(

        connection(mysql,{

            host: 'localhost',
            user: 'root',
            password : '',
            port : 80, //port mysql
            database:'nodejs'

        },'pool') //or single

    );                      
    app.get('/', routes.index);
    app.get('/customers', customers.list);
    app.get('/customers/add', customers.add);
    app.post('/customers/add', customers.save);
    app.get('/customers/delete/:id', customers.delete_customer);
    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'));
    });

1 个答案:

答案 0 :(得分:0)

这段代码依赖于快递3.4.0版本...它不能用于快递4.x然后快递4.x升级一些包和中间件......记录器('dev')不能正常工作...... var logger = require('morgan'); ...我给出了更多的想法

卸载express4和express-generator 像

uninstall
-------------
npm uninstall express -g
npm uninstall express-generator -g
install
---------
npm install express@3.4.0 -g
npm install express-generator -g
npm express -e appname   (this is express generator)  
>cd appname
appname>npm install
appname>npm install mysql
appname>npm install express-myconnection

之后替换所有ur代码复制并粘贴它  然后运行

appname>node app.js

一切顺利...........代码真的对你有帮助..