Reputation: 81
I want to host my site on https instead of http. I am using AngularJs for my Front End and NodeJs for my Back-end. I will be using https://letsencrypt.org/ for SSL certificate.
I am not able to find a way to run both web-servers on https.
Note: I do not want to separate front-end from back-end by urls.
I hope i made my point clear.
Any help is appreciated?
Upvotes: 2
Views: 1252
Reputation: 652
Usually you should put your Node service behind a reverse proxy like Apache or Nginx, and use the ProxyPass (Apache) or proxy_pass (Nginx) directives to send traffic to your Node service. Then you can use Certbot to configure HTTPS for your Apache or Nginx instance, and you don't need to configure HTTPS for your Node service (so long as it runs on the same host).
Besides making it easier to set up HTTPS, using a reverse proxy is better than using Node directly, because you can easily add additional Node processes to take better advantage of all of your CPUs.
Upvotes: 0
Reputation: 1448
I am presenting a simple application i.e.,both angularjs and nodejs works on https.First of all keep this application in server(entire folder) . Second create a public folder and place this index.html
file in it.Here is the code for it
<!DOCTYPE html>
<html lang="en">
<head>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<form>
Author:
<input type="text" ng-model="data.author">
<br>
<br> Title:
<input type="text" ng-model="data.title">
<br>
<br> Body:
<input type="author" ng-model="data.body">
<br>
<br>
<input type="submit" value="Submit" ng-click="submit()">
</form>
</div>
</body>
</html>
and here is the angular code save the file as app.js
in public folder.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$http) {
$scope.data = {};
$scope.submit= function(){
console.log('clicked submit');
$http({
url: 'https://localhost:3443/blah',
method: 'POST',
data: $scope.data
}).then(function (httpResponse) {
console.log('response:', httpResponse);
})
}
});
You need to have angular.min.js
file .Keep this file in public folder. I don't know much more about this file.Basically I am from backend side.What I understood about this is without this file we can't able to load angular code. So here is the link of the file https://github.com/Syedayesha/MEAN-Stack/blob/master/public/angular.min.js .
Next server code save the file as server.js
.Keep this file in root folder.
var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var app = express();
var fs=require('fs');
var http=require('http');
var https=require('https');
app.use(bodyParser.json({limit: '50mb'}));
app.use(express.static('public'));
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'testn'
});
connection.connect();
var options = {
key: fs.readFileSync('privkey.pem'),
cert: fs.readFileSync('cert.pem')
}
app.post('/blah', function(req, res, next) {
var cope = req.body;
console.log('request received:', req.body);
var query = connection.query('insert into form set ?', cope, function (err, result) {
if (err) {
console.error(err);
return res.send(err);
} else {
return res.send('Ok');
}
});
//res.send('received the data.');
});
var server = http.createServer(app).listen(3000);
var server1 = https.createServer(options,app).listen(3443);
console.log('server running on 3443');
Here I connected with mysqldb
. you need to give your domain ssl certificates in near options variable in server.js
. You need to have ssl certificates in root folder. You can download entire project from my github . Here is the link https://github.com/Syedayesha/MEAN-Stack.
Run the node server using node server.js
and open the browser and enter https://localhost:3443/index.html
. That's it !!! you will get the index.html
file and enter the data and check that data is stored in db or not. Actually I am working with xampp so I am using apache server. So that I run the url as localhost. You can run the url using your site name example https://example.com:3443/index.html
. And you need to change the url
in angular code app.js
as https://example.com:3443/blah
. Hope it helps.
Upvotes: 1