How to run application on url website instead of port 8080 - javascript

I followed the tutorials in AWS docs and using this video on youtube https://www.youtube.com/watch?v=BIQks_drQHM . What I am trying to do is route traffic to verus-ai.com and www.verus-ai.com. The only way I am able to run my application when I do yarn build and yarn serve is if I use the
Public IPv4 DNS
ec2-44-206-65-182.compute-1.amazonaws.com
with the port of 8080, essentially you can access it if you paste this into th e browser: ec2-44-206-65-182.compute-1.amazonaws.com:8080.
I want to be able to perform the same thing but with the domain I have i.e. verus-ai.com.
Domain Name: verus-ai.com
I was told from AWS support that the way everything is setup on Route 53 is correct and then problem resides on the application side.
I do not have much experiece with this part but there are 2 places in which the application is running on port 8080, specifically on the backend side
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
and the front end side
export default axios.create({
baseURL: "http://localhost:8080/api ",
headers: {
"Content-type": "application/json",
"Access-Control-Allow-Origin": "*",
}
});
should I remove the port 8080?

First, you need to create a CNAME record in your DNS that points your domain name, or a subdomain of it, to the EC2 instance: ec2-44-206-65-182.compute-1.amazonaws.com. Ideally you would assign an ElasticIP to the instance first so the IP doesn't change later if you have to shut it down for any reason.
Second, You need to change the backend to run on port 80 instead of 8080, because the default HTTP port is port 80 and that is the port that is used by web browsers when you enter an http:// address without specifying a port.

Related

serving client + socket io from backend

Serving a vue js + socket io client from a node js server (with socket io obviously)
while developing i was connecting and awaiting calls from localhost:8000
new Websocket(
new Server(server, {
cors: {
origin: "http://localhost:8000",
methods: ["GET", "POST"],
},
}),
);
but now, obviously, there is no longer localhost:8000 and I am serving my client's dist/index.html through my webserver as a static file
app.use(express.static(path.join(__dirname, "../../client/dist")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "../../client/dist/index.html"));
});
I feel like I am missing something really really simple? am I supposed to host my client on a different "web server" if i want to use the socket.io functionality?
as in create a simple web server for the client - and serve it from there on port 8000? and run my own server seperatly ? on port 3000?
I'm hosting this on an EC2 container on aws
I've figured it out - I need to connect to the machine's ip instead of localhost through the client's app - cause localhost:3000 doesn't actually exist...otherwise everything else is ok

Run Node JS websocket on Azure

The server app was running quite fine on Heroku but after migrating to Azure the server would not just start.
Here is the code..
const PORT = process.env.PORT || 2498;
const INDEX = '/index.html';
const server = express()
.use((req, res) => res.sendFile(INDEX, { root: __dirname }))
.listen(PORT, () => console.log(`We\'re live on channel : ${PORT}`));
const wss = new Server({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => console.log('Client disconnected'));
ws.on('message', (message) =>{
// this stays within the server
console.log('[SERVER]: Received a message => %s', message );
})
})```
.........
Clients connected are returning not establish an handshake ....
Azure web app only supports the exposure of port 80 and 443 for website access.
My previous requirements and test results:
I think your problem is a typical problem. I also had related requirements before, and the test failed in the webapp. All documents of Azure app service, talking about websocket, all use SignalR. I also tested the signalr service and it was successful, but this service is free and currently only supports 20 client connections.
What I thought before was to create a console program and put it in webjob to let webjob provide websocket service, but it all failed in the end.
Because azure webapp runs in a sandbox environment, and only open ports 80 and 443, we can't do more configuration. Obviously, the port that your websocket service starts is 2498, so it is not supported.
Suggestion
If you must use websocket, regardless of programming language, you must support custom port opening, such as using Virtual Machine.
You can compare the charges of azure cloud service and virtaul machine. Which one is right for you and which one to use.
Another suggestion is that you can buy a third-party intranet penetration tool and use a fixed IP or URL, such as ngrok, so that you can put the websocket service on your company's internal server. (The test is valid).
Of these three options, intranet penetration may be the most cost-effective choice. You can choose according to your needs.

Can't access node.js webserver with godaddy shared hosting URL

When I enter www.example.com:3000 in the browser, I receive this error (where 'example' is the name of my domain)
This site can't be reached - www.example.com took too long to respond.
I have done these things:
Installed node.js on my GoDaddy shared account
Created a folder ../public_html/testsite
Placed two files in that folder: app.js and .htaccess.
Start webserver with: node app.js
Go to browser and enter my domain's URL and port:
Receive the error message above
This post is very helpful, but I still cannot get my set up to work.
These two files are in ../public_html/testsite/
.htaccess
RewriteEngine on
RewriteRule ^index.html.var$ http://www.example.com:3000/$1 [L,P,QSA]
RewriteRule (.*) http://www.example.com:3000/$1 [P,L]
Note: index.html is the file that normally loads when you visit here
The app.js:
const http = require('http');
const hostname= '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://' + hostname + ':' + port + '/');
});
To start the webserver:
> cd ../public_html/testsite
> node app.js
Server running at http://127.0.0.1:3000
In browser, I enter:
www.example.com:3000
I expected to see, in the browser
NodeJS server running on Shared Hosting
Questions:
Should I use my own godaddy domain address for hostname in .htaccess or should it be localhost?
Is the idea to redirect www.example.com:3000 (in browser) to http://localhost:3000 (on GoDaddy server)?
I've tried all sorts of permutations (using my ip address or domain name, different port numbers, etc.)
I think I am close, but need a few ideas to try!
Port 3000, 8080, and the likes are normally used for development purposes, as in development in can be useful to have several servers running at the same time, for example one on port 3000, one on port 3001, etc.
However, on the internet, HTTP is served on port 80 and HTTPS is served on port 443. So basically, in your server implementation, you should set the port dynamically: it must not be the same whether you're running in production and in development!
I personally use the fact that on my production environment (ie for you, GoDaddy's deployment machines), the environment variable PORT is already set to 80, whereas on my local machine I don't set it, so I can write this:
const express = require('express');
const port = process.env.PORT || 3000; // 3000 on my machine, 80 on GoDaddy's server
const app = express();
app.listen(port, () => console.log(`App listening on port ${port}`));
And I access the server at these URLs:
http://localhost:3000/
http://example.com:80/
http://example.com/
The last two are the same because, as previously said, default HTTP port is 80.

how can I properly proxy requests to /api/ressource in prod?

I have a webpack dev configuration with my front end dev server running on 8080 and my backed server running on port 3000.
So in dev mode my webpack dev server is configured like follows :
proxy: {
'/api': 'http://localhost:3000',
}
How can I do the same thing in the prod server that serves the built static files of my front end ?
I have the following code for my prod server that serves the static files of my front end :
const proxy = require('http-proxy-middleware')
app.use(express.static(dir))
/**
* Redirect everything that starts with /api/* to the backend rest server
*/
app.use('/api', proxy({ target: backendUrl }))
app.get('*', (req, res) => {
res.sendFile(path.resolve(dir + '/index.html'))
})
This is not working as the cookies seem to be lost with the proxying (unlike with the proxying with webpack where evyrhthing works).
Am I going about this problem in the correct way ?
In this case, you can create a reverse-proxy which is going to receive all the information from the frontend, make the requests to the other address and then return the proper answer to the frontend. I used to develop a lot of these back in the days, there is a package that i created which can help you.
Basically the flow is:
Frontend -> endpoint on your render server (port 8080) -> backend (port 3000) -> render server (port 8080) -> frontend
You can try using:
A server (i.E. nginx) as a reverse proxy.
A node-http-proxy as a reverse proxy.
A vhost middleware if each domain is served from the same Express
codebase and node.js instance.
You may also want to check your cookie flags (changeOrigin, secure, cookieDomainRewrite etc..)
info: IF running http on localhost, the cookie will not be set if secure-flag is present in the response

Deploy PeerJS server on Heroku

I have a problem with PeerJS server. I used "Deploy to Heroku" button from here:
https://github.com/peers/peerjs-server
I have no idea how can I connect with deployed cloud.
I can't find clear documentatnion about PeerJS Server.
I don't know what is the host, port, and path for my app.
var peer = new Peer('someid', {host: 'localhost', port: 9000, path: '/myapp'});
Please advice.
This how it worked for me:
var peer = new Peer('someid', {
secure: true,
host: 'your-app-name.herokuapp.com',
port: 443,
});
Your host is simply the web address to your Heroku app. For instance, if your Heroku app is named peerjsapp, then host would be 'peerjsapp.herokuapp.com'. You can find the name of your app on your Heroku dashboard. The port is usually 9000, but can be 443 if you're using HTTPS (make sure to also pass in secure:true if you're using HTTPS). You don't need to include the path unless you've changed it; if you're running the default server config, leaving out the path on your client will automatically connect. Finally, since you're hosting your own server, you don't need an ID.
• This is how I think you should do it:
const myPeer = new Peer(undefined, {
secure: true,
host: '0.peerjs.com',
port: '443'
})
• EXPLANATION:
After deploying your app to Heroku, typed 'peerjs' into the console to search for the peerjs object, from which you can navigate and find the key-value pair of
CLOUD_HOST: "0.peerjs.com"
CLOUD_PORT: "443"
The next step is just to match your own host and port with these values.
This is how I do it Console Screenshot
• NOTE:
For the secure: true part I have tried and the app works both with and without it. So it's on you to choose to include it or not. I have also found out on https://peerjs.com/docs.html this same information, check it out if you want more detailed documentation.

Categories

Resources