NodeJS deployment to A2Hosting subdomain? - node.js

Long story short, my buddy has an A2 account (hosting his main app on PHP). I wanted to get my hands dirty with Node (particularly the deployment process) and he said I could use his hosting so I've created a subdomain. I've got the subdomain setup and running a hello world script
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var message = 'Hello world!\n',
version = 'NodeJS ' + process.versions.node + '\n',
response = [message, version].join('\n');
res.end(response);
});
server.listen();
Does anyone have experience deploying node to A2? I'm still pretty new to Node but have enjoyed it so far! Forgive ignorance on the subject, I'm coming from a full stack PHP dev setup.
What I've done so far is work through the Heroku getting started tutorial and have a decent start on the site I'm working on. Locally works great and connecting to my db instance (MySQL). I'm using EJS for templating, etc.
I'm not really familiar yet with creating createServer() functions and such as the Heroku example doesn't seem to go through it but is doing "web: node index.js" from within the proc file.
Is there a way to get the configuration on A2 to run this type of deployment?

Related

How to connect my front end to a NodeJS Backend that's deployed publicly and not on localhost

I can connect them both when my NodeJS server is deployed on localhost PORT, for example
const PORT = 9000;
const app = express()
app.listen(PORT, () => console.log(`Server is running successfully on PORT ${PORT}`))
app.use(bodyParser.json({extended: true}))
app.use(cors())
app.use(bodyParser.urlencoded({extended: true}))
app.use('/', router)
And in my front end, I can do the following:
const url = 'http://localhost:9000'
...
const res = await axios.get(`${url}/post/${path}`)
This is just an example.
But what if I wanted to deploy my NodeJS server into a heroku application, for example randomname.herokuapp.com, and I want to do
const url = 'http://randomname.herokuapp.com:9000'
...
const res = await axios.get(`${url}/post/${path}`)
It obviously doesn't work. So I'd appreciate anyone who can help me do this.
Hello first of all if you deploy an app on heroku you will have to change port,you should add this to your code
app.listen(process.env.PORT || 3000,function(){
console.log("Server started at ${PORT}");
});
because heroku will not deploy on the same port as you did on your localhost.
Furthermore if i understand your question to link front with back end,in your code you should start building paths like
app.get("/")
app.post("/")
to handle get and post request to your home root and any other root.
Also in your front pages you need to take user input,if you want text field,checkbox or something else in a form and take them in your back end ,if it is on your home page for example.
app.post("/home",function(req,res){
const input=req.body.name;
}
I have also deployed apps on heroku and the main idea still remains same,you handle get,post request with these commands.You only change port and Pocfile to run your app on heroku.Get and post routes still remain same ("/home ,/info").
The link for your app is going to be:
"https://something_given_from_heroku.com" ,which will be given automatically from heroku.And you hit other routes like /home
I hope this help you,but check other posts to be sure.
I've been doing this myself, instead of starting with barebones up to skeleton and onwards just download this base web app which you can immediately deploy to Heroku.. Link is https://github.com/hubspot/basewebapp
from there, initiate a repo on github from that link and deploy on Heroku via deployment and with automatic deploys, change on VSC or GitHub, changes apply to the webpage/webapp on Heroku.
If you need any other help, feel free to contact me.. Been surfing Heroku apps for a few weeks already.
Check www.conid.dev for as far as I've published so far

Node.js, Express.js, Angular.js: Hosting my own API

I am new to coding and I have a question about a small app that I created with angular.js that makes a post request to another express.js app.
It's a simple parentheses balance checker, you can find the whole code here:
https://github.com/OGsoundFX/parentheseschecker
The FrontEnd-Refactored folder contains the Angular.js app and the APItesting contains the express.js API.
The app is working, but I have only been using it locally. The API runs on localhost:3000 and the app on localhost:8080
But what if I want to make it public? How would I go about it? I don't really know where to start.
Where to host a node.js of express.js app. I read about AWS, would that be good, or are there better services?
I have a Wordpress website hosted on https://www.mddhosting.com/ but that wouldn't work, right?
My Angular app is calling the api locally at the moment, so I will probably have to change the API link that it is fetching:
ApiService.js
function ApiService($http) {
API = '//localhost:3000/parentheses';
this.getUser = (entry) => {
return $http
.post(API, { string: entry} )
.then(function (response) {
return response.data;
}, function (reason) {
// error
})
};
};
angular
.module('app')
.service('ApiService', ApiService);
In my API server.js
const http = require('http');
const app = require('./app')
const port = process.env.PORT || 3000;
const server = http.createServer(app);
server.listen(port);
I will definitely have to change API = '//localhost:3000/parentheses'; in my AngularJS app, but should I change const port = process.env.PORT || 3000; ?
I just need a little push start to help me clear some confusion.
Thanks!
Ensure you use application-level environment variables For example: To define the BASE_URL of your site for development and production separately. Doing so you don't have to make any configuration changes when you go live, it is a one-time process.
And if you are looking for free hosting services for pet projects Heroku is good and if you really want to make site go live for the end-users you may go for AWS EC2 instance or Heroku paid service both are good.

How to run a simple 'Hello World' app on shared hosting with Node.js?

I have been running through some basic tutorials with node.js locally on my desktop and all works fine.
I have been trying to run one of the simple 'Hello World!' apps on my 'live' hosting environment. This is not an active website, just some basic web hosting I use for trying things out. My hosting provider offers a node.js addon for a small amount extra per month which I recently signed up for.
I have amended the code in my file (named app.js) so that it references the host and port provided to me by my hosting provider rather than use localhost as it has been previously. The code looks like:
const http = require('http');
const hostname = '195.62.28.216';
const port = xxxx;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
With xxxx being substituted for the port allocated to me.
If I kick this off using my SSH client (PuTTY) by running node app.js, I get the message:
Server running at http://195.62.28.216:xxxx/
However, if I visit that URL, I just get a timeout error:
This site can’t be reached
195.62.28.216 took too long to respond.
When I connect to the host using PuTTY, using the username and password my hosting provider gave to me, it appears to default to a folder named /home/my_username. This is the location of my 'Hello World' app file.
I have contacted my hosting provider's support area, but thought I would post here too whilst awaiting a response in case anybody can point me in the right direction of what I may be doing wrong.
Many Thanks, any assistance greatly appreciated.
Thank all above for the quick responses. I think that the problem is that I was going trying to access the 195.62.28.216:xxxx URL directly from my browser but I think it's set up so that this can only be accessed from my actual web hosting environment.
In other words, I added the below to my .htaccess file on my cloud hosting account:
RewriteEngine On
RewriteRule ^ http://195.62.28.216:xxxx/ [P,L]
And when I now go to my website, I get 'Hello World!' as expected. Now I just need to figure out how to make it so that this only happens when I access a specific folder on my website and not the whole thing... :-)

Display text file using node js from a remote server

Good day! I'm having a hard time fixing this issue. I'm currently using node js webserver (http).
I'm a beginner in using node js so any help would be appreciated.
What I'm hoping to achieve is to display a string 'Hello World!' in the browser while accessing it through the URL. The problem is I'm running the script from a remote server and unfortunately I can't access it through the URL.
The script is running fine but for the browser it returns an error saying:
host didn’t send any data.
ERR_EMPTY_RESPONSE
Here is the script I'm running from the remote server:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World!');
response.end();
}).listen(2000);
I think my script doesn't have a problem. So I'm guessing it's from the setup of the server, but I don't have any idea in which part it's causing not to display it. I'm currently using a Linux Server.
Thanks in advance!
From what I can see you are listening on port 2000, are you sure that the url you are requesting the data from also contains the port e.g http://localhost:2000/ ?? Browsers by default tries to connect using port 80 on http and 443 for https, if you are listening on a different port than those, you have to define it in the url, by using a ":" after the domain/ip address
Anyway, have a look at the express module for server side rest APIs, will make request handling so much easier:
const express = require('express');
const app = express().listen(80);
app.get("/",function(request,response){
response.send('Hello World');
});
Express allows you to handle the creation of web servers better.
But node or express, do make sure that the URL you have entered consists of the port number that you have asked the server to listen to.
Another possibility is that the port you have asked to display your response is already being used by another server. You can try using a different port number.
You might have gotten the answer, but this is for the folks out there who are new to node at present and have stumbled upon this stackoverflow question! Good day :)

Node.JS local hosting server manager

I'm searching for a local hosting server manager for my Node.JS applications that lives on different ports.
All I can find is cloud-based hosting such as: nodejitsu, nodecloud, nodester, heroku and so on.
Is there any hosting environment that can run locally on my personal computer ?
EDITED:
I'm looking for something like Microsoft IIS Server Manager.
That way I can manage my different server apps on different ports/configurations.
What prevents you from downloading node.js from http://nodejs.org/ and installing it on machine of your choice? That's all you need for running node only applications.
Node.js based websites write server software in JavaScript and run it with Node. It isn't used in conjunction with a regular webserver (except when that server operates as a proxy).
If you take a look at, to pick one based on it being at the front of your list, the getting started guide for nodejitsu you will see the code to run an HTTP server in the examples.
Provided you have a public IP & domain-name set up (or use something like localtunnel), it is just a question about starting a Node.js webserver:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
(Taken from Nodejs.org.)

Resources