节点JS MongoDB POST错误

时间:2017-09-06 15:58:08

标签: angularjs node.js express mean-stack mongoose-schema

当我尝试发布到数据库时,它显示错误:'无法POST / api / postCampaign'和POST http://127.0.0.1:50945/api/postCampaign 404(未找到)。我正在尝试将数据存储在mongodb数据库中。这个代码用来工作,而mongoose是连接模数据库但是有一段时间后我试图重做代码但模数.i不再可用,所以我改变了连接URI。不确定我是否还有其他的东西

查看我的代码

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 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 个答案:

答案 0 :(得分:3)

您正在尝试向数据库发出HTTP请求。这甚至都不可能。您应该向Node服务器发出请求,而Node服务器又应该将数据写入DB。

尝试将您的请求发送到此终结点:http://localhost:3000/api/postCampaign