Cold Fire
Cold Fire

Reputation: 21

Node.js Express POST 404

I am new to Node.js (been doing ASP.Net for ages) and I cannot for the life of me figure out why I am getting this 404.

So in my work on this I have simplified the code down to just the bare minimum.

app.js for the service handler.

app.post('/getdocument', function (req, res) {
     console.log('made it');
     res.send('made it');
});

The code from the jade file

li: a(onclick="downloadfile('#{pdfUrl}');") Download PDF

And the onclick function

function downloadfile(url){
     alert(url);
     $.post('/getdocument', {data: url}, function (data) {
         alert(data);
     });
}

So I get the first alert that it made it into the function, then I get this in the log for the Node Server

POST /getdocument 404 81.910 ms - 1547

I am really at a loss as to why.

-- As Requested here is the entire 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 viewer = require('./routes/viewer');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/pdf', express.static(path.join(__dirname, 'public/pdf')));
app.use('/ViewerJS',express.static(path.join(__dirname, 'ViewerJS')));
app.use('/viewer', viewer);    
app.post('/getdocument', function (req, res) {
    console.log('made it');
    res.send('made it');
});


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

// error handlers

// 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: {}
  });
});

module.exports = app;

Upvotes: 1

Views: 1927

Answers (1)

Cold Fire
Cold Fire

Reputation: 21

So it all turned out to be an issue with Node server running. I had to restart to apply Windows updates and now it works.

Take care all!

Upvotes: 1

Related Questions