user2609157
user2609157

Reputation:

Express server receiving POST Request

Below is a Express program


PROBLEM:: I am not getting a proper JSON RESPONSE in CLIENT

WHAT I WANT TO KNOW::

HOW CAN I RESOLVE IT?


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: 'restaurants'
});

connection.connect();

// all environments
app.set('port', process.env.PORT || 1828);


app.get('/RestaurantDesc/:Key',function(request,response,next){

    var keyName=request.params.Key;
    var name_of_restaurants, RestaurantTimings;
    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM ',keyName, function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        name_of_restaurants = rows;
                        callback();
                });
        },
        // Get the second table contents
        function ( callback ) {
        connection.query('SELECT * FROM RestaurantTimings', function(err, rows, fields)

            {
                    console.log('Connection result error '+err);
                    RestaurantTimings = rows;
                    callback();
            });
        }

   // Send the 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'));
});

Any Ideas?

Upvotes: 1

Views: 2360

Answers (1)

Brett
Brett

Reputation: 3885

You do not have a route which handles a POST request. You should have something like app.post('/route', function(request, response, next) { ... });.

You will also need to use the Express body parser to get the data from the POST request i.e. app.use(express.bodyParser());. Then you can access the data off request.body in your middleware function.

Upvotes: 1

Related Questions