簡體   English   中英

快速路由未稱為節點JS

[英]Express Route Not Called Node JS

我有一個Express API,該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錯誤。

服務器上的節點js版本為6.10,用於開發的版本為8.9,是否有任何調試方法,我無法找到此問題的根本原因

謝謝

嘗試這個

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

問題在於.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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM