benzo
benzo

Reputation: 431

Node.js(Express) REST service POST method returning 404

I am struggling about one problem and that is why my post methods returning 404 error. The thing is that get method works ok, but POST no luck :(.

This is meant to be server side.

Here is code bellow and how i'm using it

taxiSnitch.js

var express = require('express');
var router = express.Router();
var cors = require('cors');

var podatki = require('../node_modules/my_modules/module_taxi');

router.use(cors());

router.post('/addCab', function (req, res, next) {
  console.log("im in");
});

router.get('/getAllCabs', function(req, res, next){
    podatki.getAllCabs(function(cabs){
        res.json(cabs);
    });
});
module.exports = router;

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var my_taxi_module = require('./routes/taxiSnitch');

var app = express();
var path = require('path');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(my_taxi_module);

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// development error handler
// will print stacktrace 
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

var server = app.listen(3000, function () {

});

module.exports = app;

enter image description here If there is any idea where I made mistake, please help me out :).

enter image description here

Upvotes: 1

Views: 1616

Answers (1)

Richard
Richard

Reputation: 3296

Your request is not a POST, it's a GET. This means that something is wrong with the code you're using to send the request, you should change the method from GET to POST.

Upvotes: 1

Related Questions