Point to node middleware without using port - javascript

I have a website, let's say mywebsite.com
I am using node and express for the middleware and point to mysite.com:3011/api
To make my middleware calls.
Hosting the site statically in ubuntu 16 (Linux) and running the middleware separately using pm2 (node server)
I would like to simply be able to do something like mysite.com/API without specifying a port in the API call.
Today I tried to demo the site at a corporate office and the apis failed due to not allowing a port to be specified in the URL.

You can use a reverse proxy (like nginx) to hide this port and forward the request to your Node.js api.
Something like this:
server {
listen 80;
...
location / {
root /path/to/static/files;
}
location /api {
rewrite ^/api(.*) /$1 break;
proxy_pass http://127.0.0.1:3011;
}
...
}

Related

Moving ExpressJS API files onto Server

So I created an simple API using ExpressJS that connects to MongoDB to perform CRUD operations. Currently I am able to get the local host running by performing command "npm nodemon" in the source folder. And it worked by testing with postman I wonder how to implement it on the server. As server runs a linux system, also I have a line of code in my root file "server.js ":
const port = process.env.PORT || 5000;
I think the process.env.port needs need to be changed in order to make it work on the server?
In addition, I did look into aws CE2 server it is so complicated that I was immediately overwhelmed. I am hoping someone can recommend dummy like me a simple and very specific solution to have a server run my scripts in ExpressJS environment. Thank you
I'm assuming your question is "How to deploy express app to a server?"
You can read some advanced topics on http://expressjs.com/, which covers some best practices, and other useful stuff. But the things you want to look at now is Things to do in your environment / setup
The important part is:
Keep your express runing on port 5000
Run your app in cluster
Run your app behind a proxy server like Nginx.
You can check this nice guide (Step 3 and 4) on how to deploy your express app to a Linux server with PM2, Nginx.
So at the end, your express app will run on port 5000 (or whatever port you desire), and your Nginx will run on port 80, and nginx will forward any request to your express app.

Single page applications Nginx Django Python and port forwarding

I want to run my single page app in production.
Since I'm using rest API and ajax calls to deliver the content from the backend to the front end for SEO reasons I need to have a no javascript version of my content available for the crawler bots.
I have a url for no javascript version of the content but since the webserver is running on port 8000 to access the no javascript files I need to navigate to mydomain.com:8000/nojs.
My apps url looks like the code below:
urlpatterns = [
url(r'^nojs/$', views.nojs),
url(r'^blog/all/$', views.allTitles),
url(r'^post/(?P<id>[\d+]+)/(?P<title>[\w+]+)/$', views.viewArticle)
]
and my nojs method is simply a hello message:
def nojs(request):
return HttpResponse("Hello")
I'm guessing I need to create some changes in my Nginx configuration to access the nojs method without using the 8000 port after my domain name.
How can I navigate to mydomain.com/nojs and get the hello message without using the 8000 port?
To access the files in the nojs folder I needed to add the following to the Nginx config file for the domain and that would forward the traffic from port 8000 to port 80.
location /nojs {
proxy_pass http://127.0.0.1:8000/nojs/; #or whatever port you are using
proxy_set_header Host $host;
}

404 when making a request to express route

I am trying to understand express and how it handles routes.
I have a domain set up with the following structure
/
app.js
/public_html
index.html
In app.js, I set up my express server:
let app = express();
app.post('/list', (request, response) => {
//get data then...
response.send(data)
});
app.use(express.static('public_html'))
app.listen(3000, function(){
console.log('listening');
});
I run the app with node app.js
Then, in index.html in the public_html directory, I am trying to request the data. I just did a simple:
fetch('/list').then(function(response) {
console.log(response)
})
But I get a 404 as the response.
I'm a bit confused about a couple of things:
My web server (Apache / Ubuntu) is set up to serve html out of the public_html directory by default. Does that mean my whole node app structure needs to be moved into the public_html folder and the actual html moved into a static folder or something?
What about the port? The node app listens on port 3000 - but I'm not sure how (or if) to make a request to that port specifically.
Route path - I am posting to /list but should it be ../list?
I haven't yet found a configuration for this app that works yet. Any help would be appreciated.
Use following code. Use ajax instead of fetch and method must be POST. Fetch is not working as it is get request by default.
Option 1
$.ajax({
method: "POST",
url: "/list"
})
.done(function( msg ) {
alert( "Data " + msg );
});
Option 2
Change only following code => POST to GET
app.get('/list', (request, response) => {
//get data then...
response.send(data)
});
Option 3
Use POST in fetch
fetch("/list",
{
method: "POST"
})
.then(function(data){ alert( "Data " + data ); })
Thanks to #vesse for suggesting option 3
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
My web server (Apache / Ubuntu) is set up to serve html out of the public_html directory by default. Does that mean my whole node app structure needs to be moved into the public_html folder and the actual html moved into a static folder or something?
Node.js and Apache can use the same static folder without conflict, but they both cannot listen on the same port. It is likely your Apache server is already running on port 80. If your Node.js server runs on port 3000, requests to port 80 will not match routes you write in your app and thus 404 will return (unless of course you had the same routes in a separate Apache hosted application).
What about the port? The node app listens on port 3000 - but I'm not sure how (or if) to make a request to that port specifically.
Since Apache is probably already listening on port 80, any request you send to http://localhost will hit your Apache server. Instead, you must make requests that include a port number, http://localhost:3000 will hit your Node.js server.
Route path - I am posting to /list but should it be ../list?
No, you should post to /list and also regard all the points Rakesh made in his answer so that you correctly match POST to POST from client to server or switch to GET if that's more appropriate. As in the second point, be sure you are posting to http://localhost:3000 and not just http://localhost. As you pointed out, one is Apache and the other is Node.js
Finally, here's the static server line of code I use when serving from a folder that is adjacent to my app script:
app.use('/', express.static(__dirname + '/public_html'));
With this, all files you put in the public_html folder become navigable in your application, which includes everything Apache related as well. Note __dirname is the always the directory from which the currently executing script is run. To visit this site, simply go to http://localhost:3000 in your browser and you should see your index file.

deploying node add on server without using localhost

I have a node app that I am trying to deploy on my server. I have an index.html file in a public folder and an app.js file. If I navigate to the project in the command line and run node app.js it runs the app on localhost:8888 and shows the index.html file.
Now that I have uploaded this to my server I am wondering what I need to do, and change (if anything) in my app.js file so that i can visit the site without visiting localhost:8888, but instead the actual url.
I have tried http://162.xx.xxx.xxx/folderName/app/public:8888, but this doesn't work.
var express = require('express')
var app = express();
app.use(express.static(__dirname + '/public'))
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(8888, function () {
console.log('Example app listening on port 8888!')
});
"Server" is a word with two primary meanings in software development.
It can mean either "A piece of software that listens on a network" or "A computer running that kind of software".
So having uploaded the JavaScript program to the remote computer that is your server you need to do exactly the same as you did on your own computer.
i.e. you need to get a terminal on the server and run node app.js
It will then be available at http://your.example.com:8888/
(More advanced uses would involve using software like forever or system.d to run it automatically as a background process).
If you were using the term server with the other meaning (i.e. you mean "Apache HTTP" or "IIS" or similar), then you are out of luck.
Using Node for server side code means running a server written in JavaScript.
To use this in combination with something like Apache, you would either:
Run the Node server instead of Apache
Run the Node server on a different port and point some services at that port explicitly
Run the Node server on a different port and use something like ProxyPass to have Apache relay requests to it
Change the port number from 8888 to 80 and then use the address of your server in the browser. For example, "mysite.com" for a domain name or "123.45.678" for an IP address.
If there are other sites on that server, you can't run it on port 80. (Port 80 is the default port websites use.) You'd need to use a different port. So, say you kept 8888 -- the address would be yoursite.com:8888

Azure Web Site starting my Hapi Node.js site with socket protocol

Whenever I deploy my Hapi.js web application to azure, it starts the server using the socket protocol (see output below).
socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb
GET /
GET /{param*}
GET /api/employees
POST /api/employees
GET /api/employees/{id}
PUT /api/employees/{id}
DELETE /api/employees/{id}
POST /api/worklog
GET /login
POST /login
Hapi server started # socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb
150914/214730.270, [response], socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb: [1;32mget[0m / {} [32m200[0m (316ms)
However, whenever I am running this locally, it starts using http... I have not run into this issue using express or loopback, only Hapi. Is there some sort of configuration that I am missing? This is the server.connection function:
var server = new Hapi.Server();
var host = process.env.host || '0.0.0.0';
var port = process.env.port || 3000;
server.connection({host: host, port: port});
The reason this is a big deal is because I cannot pass socket://*<mydoamin>* to google as a callback URI for OAuth.
You shouldn't need to pass socket://<domain> to google, you'd pass the normal https://yourDomain.com or even the https://yourSiteName.azurewebsites.net to Google for OAuth callback and it should work as you would expect.
The fact that the node application is listening on a pipe rather than a normal tcp socket is just an implementation detail of iisnode. Basically the problem is that node has it's own webserver so you can't use it with other webservers like IIS, Apache, nginx, etc. iisnode bridges the gap between IIS and node in that it allows IIS to listen to the HTTP port on the machine 80 and when IIS gets a request on that port, it just forwards it to the node process that's listening on a named pipe. This allows you to manage your sites in IIS as you normally would on a Windows Server machine, while actually writing your app in node.
You can think of it as 2 webservers running on the box, one (IIS) is acting as a proxy for the other (node) where all the work is actually happening. The fact that the iisnode developer chose to use a named pipe instead of a normal tcp socket is odd (though kind of understandable since you can't easily reserve a port per se as you can a pipe), but it's the way it is.

Categories