Calling an API to run the code in the background - javascript

I have a node.js backend program that needs to be run by calling only the API. It doesn't have frontend or UI. The program is accessing my local machine for some excel files to be uploaded in the client's database.
In my local machine, I tried calling the API, (localhost:4000/${my_route}), through postman and the program successfully run. But when I uploaded my source code in Heroku and tried to run the created application by calling the API, (https://word-word-number.herokuapp.com/${my_route}), it doesn't work.
The expectation is, the client will just call the hosted API so that their excel files which is stored on their machine will go to their database.
Do you have any ideas where to host the application and how to call the API?
Update: Here is my index.js

Heroku doesn't allow you to choose a port so you should probably get it from the environmental variables.
Let's say you are using express you should do something like this
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});

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.

How to connect frontend and backend on domain

I don't really have experience with backend and how things work together. I have created a simple live message sending app with node.js and socket.io. When I host a static web server on my machine (http-server which runs on local port using node.js) my app works perfectly fine but when I upload it on my host or github pages just for test, the backend doesn't seem to work. I uploaded all my files with an FTP program and the frontend loads fine but the backend doesn't. Do I have to know something like Django or ASP.NET to make these work on my host?
EDIT: One more thing, first line in my server.js is const io = require('socket.io')(3000)and in my script.js - const socket = io('http://localhost:3000')where 3000 and localhost:3000 stands for local host in my machine. What do i need to put instead of these?
You probably need to install and setup Node.js on your server, contact yout hosting provider for node installation if the option isn't available in yout cPanel.

GCP deployment port issue for a node js project

I have deployed one node js project in which a http server is created at port 8080 and listening at 8080 and a service url is generated.But inside http.createServer i am creating another server for a nlp engine which is listening at port 8081.So using the service url generated after deployment i am able to hit server running at 8080,but how to access the nlp engine server running at 8081 using the same service url with out using router?ordo gcp doesnt allow creation of two server listening at two ports with the same service url?
Can you try http://hostname:8081? what does it say?
If the port 8081 is open, then ideally you should be able to access that NPL engine, else you may need to get the port open in GCP.

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

What HTTP address can I send requests to from when hosting on Heroku? (client to server)

This sounds really dumb, but...
I have a Node.js Express server that used to be completely functioning on my localhost. I recently deployed it on Heroku and made some changes so my actual index.js listens to process.env.PORT instead of localhost. it listens to process.env.PORT fine through that, but I have a page rendered in React.js called app.js stored within a directory (./js/app.js) that makes HTTP requests when buttons are clicked.
However, whenever I try accessing process.env.PORT from within the app.js to make these requests, process is undefined, so everything falls apart. When working on it locally, I would just send the requests to http://localhost:3000/database, but now I have to change that since it's deployed on Heroku now.
How can I communicate between my client (app.js) and my server (index.js)?
I know this is something super simple and I've tried searching for it for the past couple of hours, but I just don't know how to phrase it.
Thank you!
Try to define your address and port in your app.js file as a heroku server address:
var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port; // put your adress here :)
console.log('Example app listening at http://%s:%s', host, port);
});
I am there if you get another pb :)

Categories