I'm having trouble trying to redirect POST requests using Node.js, Express and angular. I know that there is a standard way to use forms as follows:
index.ejs
<!DOCTYPE html> <html> <head> <title>Redirect Example</title> </head> <body> <p>INDEX PAGE</p> <form action="/redirect" method="post"> <button type="submit">CLICK</button> </form> </body> </html>
test.ejs
<!DOCTYPE html> <html> <head> <title>Redirect Example</title> </head> <body> <p>YAY REDIRECTED</p> </body> </html>
app.js
var fs = require('fs'); var https = require('https'); var express = require('express'); var bodyParser = require('body-parser'); var cookieParser = require('cookie-parser'); var app = express(); app.set('view engine', 'ejs'); app.get('/', function(req, res) { res.render('index'); }); app.post('/redirect', function(req, res){ res.redirect('/test'); }); app.get('/test', function(req, res) { res.render('test'); }); var port = process.env.PORT || 1337; app.listen(port, function(){ console.log('http://localhost:' + port + '/'); });
This method automatically redirects the page to the "test" route, since it uses the form to process the submit request.
However, using the angular method, the page is not redirected automatically. How can I do it?
index.ejs
<!DOCTYPE html> <html ng-app="project"> <head> <title>Redirect Example</title> <script src="/javascripts/jquery/jquery.js"></script> <script src="/javascripts/angular/angular.js"></script> <script src="/javascripts/angular/angular-route.js"></script> <script src="/javascripts/main.js"></script> </head> <body ng-controller="MainController"> <button type="submit" ng-click="submit()">CLICK</button> </body> </html>
main.js
var app = angular.module('project', []); app.controller('MainController', ['$scope', '$http', function ($scope, $http) { $scope.submit = function() { $http.post('/redirect'); } }]);