I have express server code below
- I want to initiate an insert statement by getting the
param
value - Then
inserting
thatparam
value into Database
What i have done so far is that i have learnt how to make JSON response ::
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'));
});
What i am trying to do::
- I am trying to find how to make a insert statement
- So that i could extract the Param value and insert that data to database
- How to modify the above code to achieve my goal
Hope i am clear !
[EDIT]
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'));
});