快递路线不再加载

时间:2017-05-21 22:12:27

标签: javascript node.js express routes js-routes

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

$('.warning').on('click', function(e) {
  var $ele = $(this).nextUntil('.warning').find('td > div');
    $ele.slideToggle();
  });
});

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

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

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

<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>

如果我首先尝试打开第二个( / info / facebook ),那么第一个( / info / linkedin )就不会打开第二个( / info / facebook )加载与 / info / linkedin 路由相关的页面。它显示了这条消息:

404 Not Found:请求的路线(&#39; linkedin-demo-app.eu-gb.mybluemix.net&#39;)不存在。

你们知道这是什么问题吗?好像它没有&#39;再次识别并找到路线。 提前致谢

1 个答案:

答案 0 :(得分: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
});