0

我遇到了快递路线的问题。这是我的情况:我有一个 node js 应用程序,在 app.js 中有以下代码

var express = require('express');
var app = express();
var path = require('path');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({
  extended: false
}));
var cfenv = require('cfenv');

// request module provides a simple way to create HTTP requests in Node.js
var request = require('request');

var routes = require('./routes')(app);

app.get('/', function (req, res) {
 res.sendFile(path.join(__dirname + '/public/index.html'));
});

var appEnv = cfenv.getAppEnv();

// compose for mysql code
var dbcontroller = require('./controller/compose-mysql-connection');
dbcontroller.databaseconnection();

const util = require('util');
// and so is assert
const assert = require('assert');

var mysql = require('mysql');

var appEnv = cfenv.getAppEnv();

// Within the application environment (appenv) there's a services object
var services = appEnv.services;

// The services object is a map named by service so we extract the one for Compose for MySQL
var mysql_services = services["compose-for-mysql"];

// This check ensures there is a services for MySQL databases
assert(!util.isUndefined(mysql_services), "Must be bound to compose-for-mysql services");

// We now take the first bound Compose for MySQL database service and extract it's credentials object
var credentials = mysql_services[0].credentials;

var connectionString = credentials.uri;

// set up a new connection using our config details
var connection = mysql.createConnection(credentials.uri);


//reading from the database
app.get("/read_fb_info", function(request, response) {

  connection.query('SELECT * FROM fb_info_table ORDER BY name ASC', function (err, result) {
    if (err) {
      console.log(err);
     response.status(500).send(err);
    } else {
      console.log(result);
     response.send(result);
    }

  });
});

app.use(express.static(__dirname + '/public'));
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
  // print a message when the server starts listening
  console.log("server starting on " + appEnv.url);
});

然后,在路由文件夹中,我有一个文件,其中包含我在应用程序中使用的其他两条路由。加载索引页面后,我有两个按钮:

<body>
    <div class="container">
      <h1>External API Usage</h1>
      <h3>LinkedIn</h3>
        <a href='/info/linkedin'>
          <img src="/images/LinkedIn_image.png" class="img-rounded" alt="LinkedIn" width="150" height="150">
        </a>
      <h3>Facebook</h3>
        <a href='/info/facebook'>
          <img src="/images/Facebook_image.png" class="img-rounded" alt="Facebook" width="150" height="150">
        </a>
    </div>

为了处理路由,我在路由文件夹中创建了一个 index.js 文件,其中包括以下内容:

retrieveFacebookUserInfo = function() {
    var deferred = Q.defer();

    var propertiesObject_FB = { id:'id', name:'name', access_token:'access_token' };

    request({url:'https://graph.facebook.com/', qs:propertiesObject_FB}, function(err, response, body) {
      if(err) {
            deferred.resolve(null);
      }
        else {
            var fb_json = JSON.parse(body);
          console.log("Get response: " + response.statusCode);
            console.log(fb_json);

            //storing information to db
            dbcontroller.writingtodb();

            deferred.resolve(fb_json);
        }
    });

    return deferred.promise;
};
app.get('/info/facebook', function(req, res){
        retrieveFacebookUserInfo().then(function(result){
                res.render('facebook.ejs', {
                    title : 'Facebook information',
                    fb_obj: result
                });

            });

        });

  app.get('/info/linkedin', function(req, res){
        retrieveLinkedInUserInfo().then(function(result){
            res.render('linkedin.ejs', {
                title : 'LinkedIn information',
                headline_linkedin: result.headline
            });
        });
  });

如果我尝试在第一个 e 打开第二个(/info/facebook)然后第一个(/info/linkedin)它不会加载与/info/linkedin路由相关的页面。它显示此消息:

404 Not Found:请求的路由('linkedin-demo-app.eu-gb.mybluemix.net')不存在。

大家知道这是什么问题吗?似乎它无法识别并再次找到路线。提前致谢

4

1 回答 1

1

您根本没有这两条路径的路由处理程序。您需要像为 /read_fb_info 路径创建它们一样:

app.get("/info/linkedin", function(request, response) {
  //do somenthing and send your response
});

app.get("/info/facebook", function(request, response) {
  //do somenthing and send your response
});
于 2017-05-22T07:35:13.160 回答