【问题标题】:Express Route Not Called Node JSExpress Route 不称为 Node JS
【发布时间】:2018-01-22 07:17:50
【问题描述】:

我有一个 Express API,它在本地环境中运行良好,但在服务器上无法运行

index.js

     var express = require('express');
var router = express.Router();
var mongoose = require("mongoose");
var version = "v1.0"
//inside local catalogues is present
console.log("File1")
var uristring =
    process.env.MONGOLAB_URI ||
    process.env.MONGOHQ_URL ||
    'mongodb://localhost/local';

// The http server will listen to an appropriate port, or default to
// port 5000.
var theport = process.env.PORT || 5000;
console.log("File2") 

// Makes connection asynchronously.  Mongoose will queue up database
// operations and release them when the connection is complete.
// mongoose.connect(uristring, function (err, res) {
//     if (err) {
//         console.log('ERROR connecting to: ' + uristring + '. ' + err);
//     } else {
//         console.log('Succeeded connected to: ' + uristring);
//     }
// });
mongoose.connect(uristring);

//Schema is not mandatory for retrieving data
var dataSchema = new mongoose.Schema({
    product: {
        category: String,
        quantity: String,
        name: String,
        price: String,
        brand: String,
        image: String,
        link: String
    }
});

console.log("File3") 

router.get('*', function (req, res, next) {
console.log("Inside JP")
    //Request URL http://localhost:3000/v1.0/?category=Pantry -> Get Category from Request
    category = req.param("category")

    //Provide the name for model ,ie collection name ->catalogue/catalogues
    var product = mongoose.model('catalogues', dataSchema);

    //Find Everything, For debugging to check if data is flowing correctly
    // product.find({}, function(err, products) {
    //     var userMap = {};
    //     products.forEach(function(product) {
    //        console.log(product)
    //     });
    // });

    //Filtering of Products
    product.find({'category': category}, function (err, docs) {
        if (err) {
            console.log('Error occured in the database');
            res.json({})
        }
        //Outputs the Data as JSON
        res.json(docs)
    });
});

module.exports = router;

控制台日志 File1、File2 和 File3 正在打印,但是当我调用 URL 时,它会抛出 404 错误。

服务器上node js的版本是6.10,开发用的版本是8.9 有什么办法可以调试,我找不到这个问题的根本原因

谢谢

【问题讨论】:

    标签: node.js api express routing


    【解决方案1】:

    试试这个

    router.get('/*', function (req, res, next) {
    . . .
    

    【讨论】:

    • 他们为什么要尝试?这是做什么的?请编辑您的答案以添加更多详细信息。
    【解决方案2】:

    问题在于 .htaccess,www 文件夹中应该有一个带有应用程序端口的 .htaccess 文件

    RewriteEngine On
    RewriteRule ^$ http://127.0.0.1:3199/ [P,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://127.0.0.1:3199/$1 [P,L]
    

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 2022-01-20
      • 1970-01-01
      • 2017-02-23
      • 2020-03-22
      • 2016-08-01
      • 1970-01-01
      • 2022-08-18
      • 2017-06-10
      相关资源
      最近更新 更多