param
值inserting
param
值进入数据库到目前为止我所做的是我已经学会了如何制作JSON响应::
var express = require('express')
, async = require('async')
, http = require('http')
, mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: '*****',
password: "*****",
database: 'DB-NAME'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 1234);
app.use(express.static(__dirname + '/public/images'));
app.get('/Name/',function(request,response,next){
var keyName=request.query.Key;
var name_of_restaurants;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM RestaurantDescription where RestaurantName = ?', [keyName], function (err, rows, fields)
{
console.log('Connection result error ' + err);
name_of_restaurants = rows;
callback();
});
}
// Send the response
], function ( error, results ) {
response.json({
'restaurants' : name_of_restaurants
});
} );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我想做什么::
希望我很清楚!
var express = require('express')
, async = require('async')
, http = require('http')
, mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: '*********',
password: "*********",
database: 'DB_NAME'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 7000);
app.use(express.static(__dirname + '/public/images'));
app.get('/Name/',function(request,response,next){
var keyName=request.query.Key;
var name_of_restaurants;
async.series( [
function(callback) {
connection.query('INSERT INTO RestaurantDescription (RestaurantName) VALUES (?)', [keyName], function (err, rows, fields)
{
console.log('Connection result error ' + err);
callback();
});
}
// Send the response
] );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
答案 0 :(得分:2)
我不熟悉您使用的库,但我认为您应该在传递给async.series
的数组中添加另一个函数。函数体应具有与上面相似的内容。
async.series( [
// Get the first table contents
function(callback) {
connection.query('SELECT * FROM RestaurantDescription where RestaurantName = ?',
[keyName],
function (err, rows, fields) {
console.log('Connection result error ' + err);
name_of_restaurants = rows;
callback();
}
);
},
// inserting a value
function(callback) {
connection.query('INSERT INTO RestaurantDescription (RestaurantName) VALUES (?)',
[keyName],
function (err, rows, fields) {
console.log('Connection result error ' + err);
callback();
}
);
}
]
将会发生两个函数异步执行,最后您仍然会向浏览器发送响应。
您已经通过var keyName=request.query.Key;
获得了GET参数。
如果您打算使用POST参数,而不是
var keyName=request.query.Key;
您应该添加一个解析变量的中间件:
app.use(express.bodyParser());
然后
var keyName = request.body.Key;