gpt4 book ai didi

mysql - 如何使用 Node.js Express 和 mysql 执行增删改查

转载 作者:行者123 更新时间:2023-11-29 23:28:05 24 4
gpt4 key购买 nike

我想使用 mysql 执行基本的 CRUD,并且我安装了一些模块,例如 npm install mysql、npm install path、npm install paths,但是我面临的一个问题是最中间件错误是我的

app.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'));
});

这是其他 customer.js

exports.list = function(req, res){

req.getConnection(function(err,connection){

var query = connection.query('SELECT * FROM customer',function(err,rows)
{

if(err)
console.log("Error Selecting : %s ",err );

res.render('customers',{page_title:"Customers - Node.js",data:rows});

});

//console.log(query.sql);
});

};


exports.add = function(req, res){
res.render('add_customer',{page_title:"Add Customers - Node.js"});
};

exports.edit = function(req, res){

var id = req.params.id;

req.getConnection(function(err,connection){

var query = connection.query('SELECT * FROM customer WHERE id = ?',[id],function(err,rows)
{
if(err)
console.log("Error Selecting : %s ",err );

res.render('edit_customer',{page_title:"Edit Customers - Node.js",data:rows});

});
});
};
exports.save = function(req,res){

var input = JSON.parse(JSON.stringify(req.body));

req.getConnection(function (err, connection) {

var data = {

name : input.name,
address : input.address,
email : input.email,
phone : input.phone
};



var query = connection.query("INSERT INTO customer set ? ",data, function(err, rows)
{

if (err)
console.log("Error inserting : %s ",err );

res.redirect('/customers');

});
});
};

exports.save_edit = function(req,res){
var input = JSON.parse(JSON.stringify(req.body));
var id = req.params.id;
req.getConnection(function (err, connection) {
var data = {

name : input.name,
address : input.address,
email : input.email,
phone : input.phone



};
connection.query("UPDATE customer set ? WHERE id = ? ",[data,id], function(err, rows)
{

if (err)
console.log("Error Updating : %s ",err );

res.redirect('/customers');

});

});
};


exports.delete_customer = function(req,res){

var id = req.params.id;

req.getConnection(function (err, connection) {

connection.query("DELETE FROM customer WHERE id = ? ",[id], function(err, rows)
{

if(err)
console.log("Error deleting : %s ",err );

res.redirect('/customers');

});

});
};
every time when i go to cmd and run the nodo app the error occur



Error: Most middleware (like logger) is no longer bundled with Express and must
be installed separately. Please see https://github.com/senchalabs/connect#middle
ware.
at Function.Object.defineProperty.get (C:\Users\Tahir\Desktop\node_modules\e
xpress\lib\express.js:89:13)
at Object.<anonymous> (C:\Users\Tahir\Desktop\nodecrud-master\app.js:23:17)
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:906:3
any body help to figure out where is the issue is ??

最佳答案

这段代码依赖于express 3.4.0版本...它在express 4.x上不起作用,然后express 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

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

appname>node app.js

祝一切顺利......代码真的能帮助你......

关于mysql - 如何使用 Node.js Express 和 mysql 执行增删改查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26795284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com