最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Express Route Not Called Node JS

运维笔记admin9浏览0评论

Express Route Not Called Node JS

Express Route Not Called Node JS

我有一个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错误。

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

谢谢

回答如下:

试试这个

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论