node.js - Express in server to perform a database query -


i want know how expressjs works in server side

i need information on server side, main things are,as per knowledge,

  • expressjs can perform functionalists of php - - - true ?
  • my client(android) ready submit post request server
  • if can send 1 single information in form of (key,value) pair, can express accept pair- - identify value based on key and, perform sql query database based on value received android client?

if can how it?

my express program ( gives response without scenario explained above - how modify program )

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' });  connection.connect();   // environments app.set('port', process.env.port || 7002); // //request first request //  app.get('/',function(request,response){     var name_of_restaurants, restauranttimings;      async.series( [         // first table contents         function ( callback ) {             connection.query('select * restaurants', function(err, rows, fields)                 {                         console.log('connection result error '+err);                         name_of_restaurants = rows;                         callback();                 });         },         // second table contents         function ( callback ) {         connection.query('select * restauranttimings', function(err, rows, fields)              {                     console.log('connection result error '+err);                     restauranttimings = rows;                     callback();             });         }     // send response ], function ( error, results ) {     response.json({         'restaurants' : name_of_restaurants,         'restauranttimings' : restauranttimings     }); } );  } );  http.createserver(app).listen(app.get('port'), function(){   console.log('express server listening on port ' + app.get('port')); }); 

hope clear thanks ,

you can send information via query params or part of url path. if send query param, can access using

req.query.keyname; 

if want send value part of url, you'll have add route accept it. can accept variable content in url using :keyname form. express capture in req.params. little this:

app.get('/some/url/:keyname', function(req, res, next){     var keyname = req.params.keyname;     // . . . }); 

then can send http request '/some/url/somekeyvalue' , variable keyname equal whatever add after /some/url/.

if you're posting data in body of request, access req.body.keyname.

edit: here's attempt @ using original code. note i'm still making values , guessing @ intent is.

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' });  connection.connect();   // environments app.set('port', process.env.port || 7002);  // //request first request //  app.get('/',function(request,response){     var name_of_restaurants, restauranttimings;      async.series( [         // first table contents         function ( callback ) {             connection.query('select * restaurants name = ' . request.body.name, function(err, rows, fields) {                 console.log('connection result error '+err);                 name_of_restaurants = rows;                 callback();             });         },         // second table contents         function ( callback ) {             connection.query('select * restauranttimings', function(err, rows, fields) {                 console.log('connection result error '+err);                 restauranttimings = rows;                 callback();             });         }     // send response     ], function ( error, results ) {         response.json({             'restaurants' : name_of_restaurants,             'restauranttimings' : restauranttimings         });     } );  } );  http.createserver(app).listen(app.get('port'), function(){   console.log('express server listening on port ' + app.get('port')); }); 

but should not query directly because of sql injection. i've never used mysql node, i'm sure there way use parameterized queries. hope more helpful.

also, i'm assuming data passed in request body, since said ready post server.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -