How to host node code in virtual directory - node.js

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.

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.

NodeJS redirect to internal localhost server

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

How to manage sub-domains in node sites

I am creating my first MEAN2 app, but I stuck in mid. My site required one front-end (mysite.com) and one back-end(admin.mysite.com) to manage content. In future I would like to have a unique URL (*.mysite.com) for all the uses who will register at my site, for which there would not be a any directory in real. How could I acchieve it in a best way. To be more clear my requirement is -
mysite.com (original site)
admin.mysite.com (a sub-domain to be managed from same server.js file)
*.mysite.com (based on request host)
Note: I have created site using angular-cli are my structure is like below
-mysite(angular 4 app)
-admin(another angular 4 app)
-server.js
This problem is not specific to node.js. However, in order to setup a domain successfully for your web app, you'll need 2 things:
Web Server
This will manage for you virtual hostnames etc. I would advise you to look at setting up nginx as a reverse proxy in front of your node app.
Domain provider
This will actually allow you to publicly use the domain *.mysite.com. Then you will have to setup a DNS Record, generally an A record, to point at your server IP Address.
This is very high level :) but should be enough to get you started.
EDIT
If you want to test locally just setup nginx with a test server name to proxy_pass to your local nodejs app (i.e. http://localhost:3000) and create a host entry in your hosts file for that hostname.

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.

Changing localhost server files are served from using Node (Webstorm /maybe IntelliJ)

I'm not sure what I'm missing here, so hopefully someone can help me out. I'm working on a project where we're using Node and in the Run/Edit configurations I've down the following:
Node interpreter: This is the path to the node.exe file
which I checked out from Subversion
Working directory: this is where the "app.js" file is, this is the
path that from the command line you type node app.js and it starts the server
JavaScript file: app.js This is the name of the file that actually creates the server
Now from the main nav bar when I do Run / Run my server the box at the bottom pops up and tells me that Express server is listening on port 3000. Cool.
I can navigate to localhost:3000/myPage.html and I can get to the page just fine.
I added as JSON file to the same directory on my hard drive that myPage.html is in, and I can navigate to that as well by localhost:3000/largeTestData.json.
So the server is up and running and serving file as it should. My problem is that in my Webstorm project, I want to make an AJAX request to that largeTestData file. I do so using jQuery like:
var data = $.get('localhost:3000/largeTestData.json');
data.done(function(data){
console.log('here is your data');
cnosole.log(data);
})
When I do that I get the error (in Chrome)
XMLHttpRequest cannot load localhost:3000/largeTestData.json. Cross origin requests are only supported for HTTP.
and so I look at the URL and I'm seeing:
http://localhost:63342/
Obviously Webstorm has started the server correctly, but when I view an HTML file, it's not using that server (which, of course is why I'm getting the CORS error.
There's some fundamental stuff here which I'm obviously not getting. I need my IDE to deploy to the Web server that it started up, but it's not doing that. Please, someone give me a once over on all the technologies that I'm missing out on here.
WebStrom didn't start your node.js server, but serves static pages by its own internal HTTP server which doesn't know anything about node.js and Express.
The main problem:
When you start your node.js server, it's serving JSON files on port 3000. If you open an HTML-page with the little menu in WebStorm (where you can choose the browser), WebStorm opens the browser with an URL pointing to its own internal webserver running on a different port (e.g. 63342). JavaScript security prohibits loading data from a different host/port Same-origin policy.
It's not WebStorm's fault and you need a solution for this problem in production or you can't go live.
General Solution:
Either you have to ensure that HTML pages and JSON data come from the same host+port, or you can circumnavigate with (a) setting server-side headers ('Access-Control-Allow-Origin: *') as #lena suggested, or (b) using JSONP. Below you find some thoughts using nginx as a reverse proxy so from browser's point of view all requests go to the same host+proxy. It's a very common solution, but as mentioned above, there are other options.
Primitive solution:
Don't use WebStorm to open your browser. Load the page from http://localhost:3000/ and change the URL of the REST resource to $.get('/largeTestData.json'). You'll miss some comfort from your IDE, but you can immediately see that your program is working.
Comfortable solution:
As #lena suggested, there is a way to configure your Express/node.js as a server known to WebStorm. I haven't tried it, but I suppose you can then just press the Run-button and maybe the node.js plugin in WebStorm is as intelligent to know the static-maps in Express and know how to map an HTML-file to a web application URL and open the page in the browser with the URL served by your node.js application. (I'd be surprised once again if this really works magically, but maybe you can configure a mapping from files to URLs manually, I don't know.)
Dirty solution
With some options you can disable security checks, at least in Google Chrome. Then it's possible to load JSON data from a different port than your HTML page. I wouldn't recommend using these options (just my opinion).
Additional Hints
If you do more than just playing around with node.js and some UI fun and you have to serve your application "production-ready", then have a look at nginx to serve your static files and reverse proxy node.js requests from there. I'm using this setup even for development and it works like a charm.
Of course node.js / Express is able to serve static files as well, but IMO placing something like nginx in front of node.js (clustered) bring a bunch of advantages for production sites, e.g. load-balancing, ssl-offloading, avoid JSONP, in many cases performance, easier deployment updates, availability.
To get your code working, just change the URL in $.get() to full URL (including protocol):
var data = $.get('http://localhost:3000/phones.json');
In Webstorm 2016.3 (and probably earlier) there is now another option. Under the Configuration Settings for NodeJS runs, one can manually set the page and port to be loaded via Webstorm's "Browser/Live Edit" settings.
See the screenshot below for settings one can change.

Resources