【发布时间】: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