2

I'm making an ionic app for android and today I implemented a server side nodejs (express) Restful API with mongodb on cloud9.

But when I trigger a http request to the url, I keep getting the error mentioned in the title:

This is my angular http request on ionic (simply to test first):

app.controller('DashCtrl', function($scope, $http, $q) { 
    $http.get('https://[workspace]-[user].c9users.io/api/capture')
        .then(function(result) {
            console.log(result);
    });
});

This is an image of my workspace:

workspace

I used the following code to make the database: api.js

var Capture = require('../models/capture');
module.exports = function(router) {
    router.get('/capture', function(req, res){
        var capture = new Capture();
        // capture.birdname = req.body.birdname;
        // capture.place.city = req.place.body.city;
        // capture.place.country = req.place.body.country;

        capture.birdname = "Pigeon";

        capture.save(function(err, data){
            if(err)
                throw err;

            res.json(data);
        });
    });

    router.get('/captures', function(req, res){
        Customer.find({}, function(err, data){
            res.json(data);
        })
    })
}

capture.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var captureSchema = mongoose.Schema({
    birdname: String,
    place: {
        city: String,
        country: String   
    }
});

module.exports = mongoose.model('Capture', captureSchema)

database.js

module.exports = {
    'url': 'mongodb://' + process.env.IP
}

server.js

var express = require('express');
var app = express();

var mongoose = require('mongoose');
var configDB = require('./server/config/database.js');

mongoose.connect(configDB.url);

var api = express.Router();
require('./server/routes/api')(api);
app.use('/api', api);

app.listen(process.env.PORT, process.env.IP);
console.log('Listening on port ' + process.env.PORT)

Anyone have an idea why I'm getting this error and what I need to do to fix this? I'm guessing it has something to do with the server allowing me to send requests to the API though I don't know how to implement that.

Is there also a way to secure my database (API key maybe) and how would I implement this?

Thanks in advance

Pex
  • 519
  • 2
  • 12
  • 30
  • Possible duplicate of [Response for preflight is invalid in angular](https://stackoverflow.com/questions/37481764/response-for-preflight-is-invalid-in-angular) – Paul Sweatte Jun 21 '17 at 14:50

0 Answers0