I am working on project containing app and landing pages. We are using Nodejs with Axios and VueJs for app part. But for landing pages, it is simple jQuery. I must do some API calls for landing pages, but I am not able to use NodeJs result in jQuery to serve data in my landing pages. I am new at NodeJs and these technologies. Here are my codes:
my Routes :
const router = express.Router(); ... router.get('/api/items', myApiController.getItems);
NodeJs controller
module.exports.getItems = (req, res) => { const response = myApiController.getItems(); if (response.status === 200) { res.send({ status: 200, data: response.data }) } else { res.send({ status: 404, data: null }) } }
my main script :
$.get("/api/items", function(data, status){ alert("Data: " + data); var mylist = $("#mylist"); $.each(data, function(item) { mylist.append($("<option />").val(item.name).text(item.name)); }); });
Even if I am getting status:200 the nodejs is returning HTML of page 404. I do not find the cause, And honestly I do not understand the reason. It seems it is try to get a page does not exist, but I am requesting a json from function. I try to replace contoller method call by a trash json but nothing work. Here is what I try:
router.get('/api/items', function(req, res){
console.log('cc');
return res.json([{
'toto': 'toto',
'tata': 'tata',
}]);
});
It seems routes definitions issue, but I do not know how to fix. Could this have something with express Router ? Could you please explain me and help me to fix this? Thanks