Express to generate JSON

前端 未结 3 1246
滥情空心
滥情空心 2021-01-17 00:46

I am trying to pass a key value and generate a JSON response based on the key passed

Express Program

var express =         


        
相关标签:
3条回答
  • 2021-01-17 01:29

    try to refer the following links may help

    http://book.mixu.net/ch10.html

    http://expressjs.com/api.html

    0 讨论(0)
  • 2021-01-17 01:40

    You have two handlers for the path /RestaurantDesc/ - only one should be kept.

    In your query, the syntax is wrong.

    Assuming you have a table that looks like this (change these names to match your schema):

    Table Name: Restaurant
        column:       name
    

    And you are using node-mysql, Change your query to something like this:

    connection.query('SELECT * FROM Restaurant where name = ?', [keyName], function (err, rows, fields) {
        console.log('Connection result error ' + err);
        name_of_restaurants = rows;
        callback();
    });
    

    The ? is a place holder for values passed in the array of values passed in the second parameter to query.

    See: Escaping query values for more information on how this works. Short answer though is that the values will be escaped properly using this approach.

    0 讨论(0)
  • 2021-01-17 01:42

    The path that does what you want is /, not /RestaurantDesc/. Look at the first parameter to app.get().

    Here's what I get with http://54.218.73.244:7004/

    {
      "restaurants": [
        {
          "restaurantID": 1,
          "restaurantNAME": "CopperChimney",
          "url": "http://54.218.73.244:7002/CopperChimney"
        },
        {
          "restaurantID": 2,
          "restaurantNAME": "Aroy",
          "url": "http://54.218.73.244:7002/Aroy"
        },
        {
          "restaurantID": 3,
          "restaurantNAME": "MarkBoulevard",
          "url": "http://54.218.73.244:7002/MarkBoulevard"
        },
        {
          "restaurantID": 4,
          "restaurantNAME": "Indian",
          "url": "http://54.218.73.244:7002/Indian"
        }
      ],
      "RestaurantTimings": [
        {
          "_id": 1,
          "RestaurantTime": "8pm to 11pm"
        },
        {
          "_id": 2,
          "RestaurantTime": "10pm to 12pm"
        },
        {
          "_id": 3,
          "RestaurantTime": "11pm to 9pm"
        },
        {
          "_id": 4,
          "RestaurantTime": "10pm to 5pm"
        }
      ]
    }
    
    0 讨论(0)
提交回复
热议问题