Reputation: 559
I can't get my server to call a function from the client using node.js and express. I don't need to pass data, I just need to alert to the server that it should call a function. This is what I have (after following numerous tutorials):
client:
$.ajax({
type: 'POST',
url: 'http://localhost:3001/admin',
sucess: function() {
console.log('sucess');
}
server:
var express = require('express');
var app = express();
var server = require('http').Server(app);
app.set('port', process.env.PORT || 3001);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.post('/admin', function(req, res) {
console.log("admin refresh");
res.send(200);
});
Error:
POST http://localhost:3001/admin 404 (Not Found)
Upvotes: 1
Views: 5302
Reputation: 146134
You have your middleware in the wrong order. Express prossess them in order declared. Move your app.post('/admin...
line ABOVE your Not Found middleware.
Upvotes: 6