I am asking this question because after searching the internet, I cannot find anything about this. It may be that I simply should not even be asking this question, but it's driving me crazy so here we go.
I have a Heroku application with two dynos. I have a web dyno with my frontend (Angular), and a server dyno with my backend (Node). I want to send an HTTP request from my frontend to my restful backend. Is this possible?
I know that I can get this to work by separating these into two apps, but I would like to keep them in one app if possible.
Here is my Procfile just as some additional information. They are deploying successfully, I just want to give a visual on what I am deploying exactly.
web: cd frontend && npm start
server: node app.js
If this is not even a possible thing to do, any helping hand would be appreciated from someone more knowledgable than I.
Possible. Example:
const http = require('http');
const path = require('path');
const express = require('express');
const WSServer = require('ws').Server;
const DateFormat = require('dateformat');
let wss;
let server;
const app = express();
app.use(express.static(path.join(__dirname, './../build')));
server = new http.createServer(app);
wss = new WSServer({ server })
this.wss = wss;
wss.on('connection', function(socket) {
console.log(DateFormat(new Date(), 'm/d h:MM:ss TT'),
`client connected to server (${wss.clients.size} total)`);
socket.on('message', function(data) {
console.log(data)
});
socket.on('close', function(code, desc) {
console.log(DateFormat(new Date(),
"h:MM:ss TT"),'client disconnected, total:', wss.clients.length);
});
});
wss.on('listening', () => console.log('Websocket listening on port', config.get('port')));
wss.on('error', err => console.log('Websocket server error:', err));
server.on('error', err => console.log('Server error:', err));
server.listen(process.env.PORT);
Here I am deploying a static front end which is located at ../build
and I serve it. I'm extending the web server with websocket functionality running on the same address but under a different protocol.
You can also do it with only http. You have to separate the routes.