0

When I try to post to the DB it's showing Error: 'Cannot POST /api/postCampaign' and POST http://127.0.0.1:50945/api/postCampaign 404 (Not Found). I am trying to store data in mongodb database. This code used to work and the mongoose was connected modulus database but after sometime I am trying to redo the code but the modulus.io is not available anymore, so I changed the connect URI. Not sure if there's anything else I am missing

See my codes

server.js

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var port = process.env.PORT || 3000;

//Database connection
mongoose.connect('mongodb://localhost/myappdatabase');

app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/dist'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(morgan('dev'));

//Database Schema / Model
var Schema = mongoose.Schema;

var campaignSchema = new Schema({
    goalAmount: String,
    campaignTitle: String
});

var Campaign = mongoose.model('Campaign', campaignSchema);

//Routes
app.get('/startCampaign', function(req, res){
        res.sendfile('./public/views/start-campaign.html');
});

app.post('/api/postCampaign', function(req, res) {

    Campaign.create({
        goalAmount : req.body.goalAmount,
        campaignTitle: req.body.campaignTitle,
        done : false

    });
});


app.listen(port);
console.log('App listening on port' + port);

MainController.js

appTitan.controller('mainController', ['$scope', '$http', function($scope, $http){
    // Signup form
    $scope.form = 'signup';

    $scope.switchForm = function(newForm){
        $scope.form = newForm;

    };

    // Start camapaign
    $scope.startCampaignData = {};
    $scope.createCampaign = function(){
        $http.post('/api/postCampaign', $scope.startCampaignData)
            .success(function(data) {
                console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };
}]);

form page

<form action="">
    <div class="form-group">
        <label for="goal_amount">How much money would you like to raise?</label>
        <input type="text" name="goal_amount" id="goal_amount" ng-model="startCampaignData.goalAmount">
        <select name="goal_amount" id="goal_amount">
            <option value="GBP">GBP</option>
            <option value="USD">USD</option>
        </select>
    </div>
    <div class="form-group">
        <label for="campaign_title">What's the tile of your campaign?</label>
        <input type="text" name="campaign_title" id="campaign_title" ng-model="startCampaignData.campaignTitle">
    </div>
    <div class="form-group">
     <label for="goal_amount">Choose a category</label>
       <select name="campaign_category" id="campaign_category">
            <option value="">Choose a category</option>
            <option value="USD">USD</option>
        </select>
    </div>
    <div class="form-group">
        <button ui-sref="form.stepTwo">CREATE</button>
        <button type="submit" ng-click="createCampaign()">Submit Test</button>
    </div>
</form>

1 Answer 1

3

You are trying to make an HTTP request to the database. That's not even possible. You should make a request to your Node server which in turn should write data to DB.

Try to send your request to this endpoint: http://localhost:3000/api/postCampaign

Sign up to request clarification or add additional context in comments.

4 Comments

I've added that to both server.js post and angular post but it returns: net::ERR_CONNECTION_REFUSED and Error: null
Sorry, I didn't understand what you've changed. I think that the code from your question will work if you only change your API url from http://127.0.0.1:50945 to http://localhost:3000
Thank you for your reply @poohitan. The mongoose.connect URI I sent is just a sample. Where I am actually trying to store the data is mongodb://<myusername>:<mypassword>@123456.mlab.com:222288/x‌​xxxxx. Do I still need to change the API url?
apologies I have solved the issue. Just realised that I didn't run the server. But your answer lead me to understand that, Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.