This is a very basic question but I am unable to find the answer. If its a duplicate, kindly link me to the answer, apologies.
I have two urls http://127.0.0.1:8080/
and http://127.0.0.1:8080/foo.htm
The first one has a button and a text field. When the button is pressed, the data in the text field is sent as a POST request to http://127.0.0.1:8080/foo.htm
. I want the text field data to be shown in foo.htm file.
When the button is clicked, the url as well as the view should be changed. I have this so far.
var bodyParser = require('body-parser')
var express = require('express');
var app = express();
var serveStatic = require('serve-static')
var webpageDir = "/webpages"
var compression = require('compression')
//Sets up the basic express server
app.use(express.static('public'));
app.use(compression());
app.use( bodyParser.json());
//sets up the port on which server will listen
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
//Home page, basic url
app.get('/', function (req, res) {
res.sendFile(__dirname + webpageDir+"/"+"main.htm");
})
// post request from main.htm gets sent here.
app.post("/foo.htm", function(request, response){
//foo.htm should be displayed instead of main.htm with the input text
response.sendFile(__dirname + webpageDir+"/"+"foo.htm");
})
Following is main.htm
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<title> Landing Page </title>
<style>
body {
background-color: linen;
}
</style>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
</br>
<input ng-model="inputName" type='text' placeholder="Type your name here">
</br></br>
<p>Response as JSON is {{getResponse}} </p>
<button ng-click="getRequest()">Send POST Request to /foo.htm </button>
</hr>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope, $location, $http, $window){
$scope.getRequest = function(){
var inputText = $scope.inputName;
$http({
method: 'POST',
url: 'http://127.0.0.1:8080/foo.htm',
data : {sampleTextSent :inputText }
}).then(function successCallback(response) {
$scope.getResponse=response;
// The html code from foo.htm is being sent here
// I want the URL to change and inputText to be sent
// I can use $window to change the url but not data
}, function errorCallback(error) {
$scope.getResponse=error;
})
}
});
</script>
</body>
</html>
And at last foo.htm
<!DOCTYPE html>
<html>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<head>
<title>GET request demonstration</title>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<h1>{{dataThatWasSentHere}}</h1>
</div>
</body>
<script>
var app = angular.module("myApp", []);
app.controller("myController", function($scope){
$scope.dataThatWasSentHere = // the input text was main.h
})
</script>
</html>
If someone could provide me any hints or lead me to some resources, I would really appreciate it. Thanks in advance!