使用express进行简单的JSON响应

时间:2013-08-23 04:14:16

标签: node.js express

我使用express来生成JSON响应

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

我的错误为::

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

如何解决此问题

1 个答案:

答案 0 :(得分:0)

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