NodeJS redirect to internal localhost server - node.js

I am new to nodejs so apologies if this is a bad question.
I am using NodeJS + Auth0 to authenticate a shiny webapp. I have most of it working now including the call back but am stuck on the last step.
Assuming it authenticates, I want to proxy pass directly to 127.0.0.1:3838/myapp.
In other words, if the loopback address were public I could do:
res.redirect('http://127.0.0.1:3838/myapp/');
However, as it's not public that is no longer an option. Any advice?

res.redirect('/myapp'):
Should redirect you to the /myapp route assuming that you are running the NodeJS app locally on 127.0.0.1:3838
Have a look at https://stackoverflow.com/a/44445540/16471349

Related

Directus on remote server

I have an app on remote server. I use directus of this app and I have a site example.com. I want to make able the route example.com/admin manage content . I try to start directus with command pm2 start npm --start, but I get this enter image description here
Can you help me?
You would be better off serving Directus on a subdomain (admin.example.com), this will separate the back-end from the front-end. The root URL for the Directus install is the API URL, followed by /admin for the editing interface.
If you have installed Directus under /admin then the API will likely be available at example.com/admin and I believe the interface will be under example.com/admin/admin, which is why I suggest a subdomain.
I generally use mgmt.example, cms.example or similar variations.

Detect if user uses proxypass in Apache or NodeJS

I have developed a NodeJS app running on port 1234. In order not to have ":1234" in the address bar, I use proxypass/proxypassreverse so that the URLs look like "mysite.com/myapp/".
Everything works fine but my app is behind a SSO. When a user tries to reach "mysite.com/myapp", he's redirected to the SSO, he logs in, then the SSO redirects him to "mysite.com:1234" (because the SSO doesn't understand that there is a proxypass).
I created a javascript on my page detecting if there is ":1234" in the URL and if so, the window.location replaces ":1234" by "/myapp".
This is not a very good solution for me, I think there may be a NodeJS or even better an Apache solution to detect if the URL reached is the ":1234" one or the "/myapp" one.
Thanks in advance.
OK my problem was that NodeJS sent to the SSO the URL with the port number. All I needed to do was to replace the port number by the string I use for the ProxyPass:
req.get('host').replace(':' + PORT, '/node')

How to host node code in virtual directory

I have 2 separate code of node application. I have already deployed node code on a windows server. Code runs on port 80 so when I hit my domain like www.myproject.com it loads my home page of my node server.
When I hit www.myproject.com/admin it should give me the admin page form my other node js code.
You are a bit short on information in your question, but I'll do my best with what you've given. Assuming you have 2 instances of node on your machine, but want a user to only access one, and you are using express, I would suggest an http proxy on one to route to the other such as:
https://www.npmjs.com/package/express-http-proxy
Setup your /admin route to access your uri of your 2nd instance. Hope that helps.

Messenger bot - Sample code

I am trying to create a bot. Using this project. My setup is as follows:
Amazon EC2 instance. I created a sub-domain.domain.com I have added the ssl certifications. I have pointed the sub-domain to the /var/www/sub-domain/
I have installed the unzip and installed the project in /var/www/sub-domain/project/ I have configured the project and correctly run it:
Node app is running on port 5000
If I access http://sub-domain.domain.com:5000 I access to the projects public index. I understand that means the port is open and the node app works.
Now when I am trying to configure in facebook my webhook I dont understand what url callbak to use. From what I understand in the configuration the server url whould be https://sub-domain.domain.com and the configuration should work. but it doesnt.
What url should I use?
Your callback URL should be your actual file where facebook would send user data. Like if you were using PHP, you'd say: http://yourhost/chatbot.php. Currently, webhook would be the URL on which your node app is listening.
Your webhook needs to an accessible URL. I don't know which sample code you used, but my guess is that with your current set up you should use http://sub-domain.domain.com:5000/webhook.
You can use Apache or Nginx to make it accessible on http://sub-domain.domain.com/webhook (port 80) with ProxyPass/proxy_pass directives.
I have installed the unzip and installed the project in /var/www/sub-domain/project/ [...] I access to the projects public index
The Javascript files of your project don't need to and should not be accessible to the public, as they may contain your application's secret IDs and tokens.

Route issue in Node JS with express

I have a godaddy domain being redirected to an instance of EC2 with an elastic IP. on instance there runs my NodeJS on port 5000. i m redirecting 80 to 5000( till here everything works fine). In express i have a route say '/Calculate/:Type', which is visited when some caculate button is pressed. ideally the when button is pressed a request to server should go like http://example.com/calculate/3 but here is goes http://example.com//calculate/3 an extra / before caculate ruins the entire route. can someone please tell me why is it happening?
Modify the route where you define the findone query and remove the forward slash from it.
Try adding a period in front of /Calculate/:Type so
'./Calculate/:Type' in your express route
Ahh so it seems like you may be hitting a CORS error. Is your webserver and dataserver on the same url?
Best option : Put the node instance serving your app on yourdomain.com, and make your api endpoint api.yourdomain.com/Calculate/:Type
Other Option: exposing yourself to security risks by adding a 'Access-Control-Allow-Origin = *' in the header of your response
Other Option: expose yourself to security change your settings in htaccess to allow all, or the specific domain

Resources