0

Here are mistakes shown in console, i can't understand why http adress in is not found

POST http://localhost:3000/api/message 404 (Not Found)                angular.min.js
XHR finished loading: POST "http://localhost:3000/api/message".       angular.min.js
Error "Cannot POST /api/message\n"                                    controllers.js

my controllerjs:

testControllers.controller('SecCtrl', ['$scope', '$http',
   function SecCtrl($scope, $http) {       
      $scope.message = '';
      $scope.saveInput = function() {              
        $http({
            method: 'POST',
            url: 'http://localhost:3000/api/message',
            headers: {'Content-Type': 'application/json'},
            data: JSON.stringify({message: $scope.message})      
        }).

        success(function(response) {
            console.log("Success " + JSON.stringify(response));
        }).

        error(function(response) {
            console.log("Error " + JSON.stringify(response));
        });
     };
}]);

server.js code, and after i send input data to server i try to save it into mongodb using mongoose:

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

var Message = mongoose.model('Message', {
    message: String
});

mongoose.connect('mongodb://localhost:27017/test', function(err) {
    if(!err) {
        console.log('connected to mongo');
    }
});

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

app.use(function(res,req,next) {
    res.header("Access-Control-Allow-Origin");
    res.header("Access-Control-Allow-Headers", "Content-Type,   Authorization");

    next();
})

app.get('api/message', function(req,res) {
    Message.find(function(err,message) {
        if(err) {
            res.send(err);
        } else {
            res.json(message);
        }
    })
})

app.post('api/message', function(req,res) {
    var message = new Message(req.body);
    message.save();

    res.status(200);
})

app.get('/', function(req,res) {
    res.sendFile(__dirname + '/index.html');
})

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

1 Answer 1

1

I think it should be with absolute path /api/message:

app.post('/api/message', function(req,res) {
    var message = new Message(req.body);
    message.save();

    res.status(200);
});

Look at here http://expressjs.com/en/api.html#path-examples

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

2 Comments

i tried to write same path in controller, but it causes same mistakes
they are the same, i posted them at the beginnig of topic

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.