简体   繁体   中英

Using express for simple JSON response

I am using express to generate a 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: 'xxx',
    password: "xxx",
    database: 'db'
});

connection.connect(); 

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




//
//REQUEST FOR FIRST REQUEST
//





app.get('/',function(request,response){
    var restaurants, collegeTimings;

    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM restaurants', function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        val1 = rows;
                        callback();
                });
        },
        // Get the second table contents
        function ( callback ) {
        connection.query('SELECT * FROM collegeTimings', function(err, rows, fields)

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

   // Send the response
], function ( error, results ) {
    response.json({
        'val1' : restaurants,
        'collegeimings' : collegeTimings
    });
} );



//
//REQUEST FOR SECOND REQUEST
//


app.get('/class',function(request,response){
    var clas, collegeTimings;

    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM college', function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        college = rows;
                        callback();
                });
        },
        // Get the second table contents
        function ( callback ) {
        connection.query('SELECT * FROM RcollegeTimings', function(err, rows, fields)

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

   // Send the response
], function ( error, results ) {
    response.json({
        'college' : class,
        'collegeTimings' : collegeTimings
    });
} );


} );

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

I am having the error as::

SyntaxError: Unexpected end of input
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

How to resolve this

app.get('/'...缺少另一个});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM