我有一个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) {
. . .
问题出在.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]