I have been struggling with this for a while now and can’t find anything on the Internet about it, so I was wondering if somebody has any experience getting data in Ionic with Sequelize and the routing with Express.js.
This is wat I got:
app.js
var express = require('express');
var app = express();
var path = require('path');
var routes = require('./routes/index');
var cars = require('./routes/cars') (app);
app.use('/cars', cars);
routes/cars.js
var models = require('../models');
var express = require('express');
var router = express.Router();
router.get('/cars', function(req, res) {
models.Car.findAll()
.then(function(data){
res.json(data);
})
.catch(function(error){
console.log("ops: " + error);
res.status(500).json({ error: 'error' });
});
});
module.exports = router;
models/cars.js
"use strict";
module.exports = function(sequelize, DataTypes) {
var Car = sequelize.define("Car", {
name: DataTypes.STRING,
favorite: DataTypes.INTEGER
}, {
freezeTableName: true,
tableName: 'car'
});
return Car;
};
I have tried multiple ways to get this to work, but when I go to http://localhost:8100/cars the browser outputs:
Cannot GET /cars
Can anyone please help me?