how to run / execute app deployed on heroku - javascript

I am new to node.js and heroku.
By following docs and some tutorials I have successfully deployed my app on heroku.
The problem I am facing is that I don't know how to run that deployed app on heroku. After reading a number of tutorials, its still not clear to me.
When I run the commands
$ git add .
$ git commit -am "make it better"
$ git push heroku master
from local command line, it executes the scripts. But when I click on the open app from the heroku account, it does not execute all of files.
I am running 1 web dynos and heroku shows that my web dyno executes following command
web node build/server.js
The server.js file contains following code
require('./routes/main.routes.js');
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("this is a test page");
}).listen(process.env.PORT);
when I click on open app from heroku, it opens the app in the browser with message
this is a test page
It does not run the main.routes.js file.
But when I click on restart all Dynos, the main.routes.js file also gets executed. I want the app to execute the main.routes.js file when I click on open my app or refresh it in the browser window.
Is the restart all Dynos only way to execute script? or I have done something wrong?
Any help will be much appreciated.

You can try this replace the comment (//write the code here) and try requesting the url:
var http = require('http');
http.createServer(function (req, res) {
// write the code here if it needs to execute every time
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("this is a test page");
}).listen(process.env.PORT);

Related

Failed to connect to localhost port 8000: Connection refused in Node.js

I'm trying to connect to the HTTP server in node.js.
I've spent my whole day searching online to solve my issue but unfortunately, it is of no use.
This is a simple code I wrote taking reference from official Node.js documentation.
Index.js
const http = require('http');
const requestListener = function (req, res) {
res.writeHead(200);
res.end('Hello, World!');
}
const server = http.createServer(requestListener);
server.listen(8080);
The code I am running inside CMD terminal
curl localhost:8080
This returns the following error:
I know this questions have been asked a thousands time in stack overflow but none fixed my issue. Anyone can help me?
In you code you are listening to port 8080 but from commandline you are trying 8000. Make sure you are using correct port and ensure its not in use.
You are trying to execute C:\Users\prabi\app\server.js when the real file is C:\Users\prabi\app\index.js so verify the start script in package.json and fix it.
Fire up another terminal and run the command:
node index.js
Then go back to your previous terminal window/tab and run curl localhost:8080 again. Should work.
The problem is that your code has been written well, but you aren't executing it at all. So if you don't run your node code, your http server cannot listen at the 8080 port in the first place.

how to make a node js run a js file and issues creating a js file in node.js console window with win10

I am a beginner in node js and have a couple of months experience with javascript, have finished codecademy js and jquery introductory courses and learned more about js in the internet and have an understanding of html and css in intermediate level or near to intermediate level. I am a complete noob in getting the js and node js to run alltogether. Using VS code for text editor. Node.js is installed from their original homepage, using node.js APP. File named global js.is sticked to 2 folders: the same with node.js and desktop folder for vs code files( the ones I create in VS code as projects or just simple files). The darn thing just doesn't make sense to me and I dont get this mess to work either. To be more specific, then:
I have 2 issues:
Firstly, did an install: npm install javascript bla bla. It was a sucsess, mkdir and cd were successful. NUL >introduction.js says access denied.
Alos I have a file named global.js. It is sitting in the same foler as node.js + the original version of this fie is in the same folder of desktop vs code's excercises. I can't make my node.js open this global.js file.
How do I overcome of these issues?
I tried to use this as a guide line: How to run a hello.js file in Node.js on windows?.
Did following parts of that above: changed account type to ADMINISTRATOR via appwize smth from run. Called cmd from run after having given myself and admin status permanently and windows logged me off and back on again. Tried to run the file from command prompt, declaring the exact path to file in cmd. It reached to the path, showed no error, went to the file from there. No errors, NOTHING didn't happen... Tried the global install whatever faced an issue in there and got stuck with it.
I need some help in here! Would be nice if someone could explain to me what is wrong and what's the basic concept of using node.js or what are the alternative ways to programmetely launch it?
Do I need to use the node.js console or node.js app?
CODE IN main. js :
var http = require("http");
var path= require ("path");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
console.log (`Rock on World from ${path.basename(__filename)}`);
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
No need to use any special console, on windows, standard cmd is enough.
Check if node is installed properly first. Run node -v and see if it prints its version.
Create a folder for your project and make sure your are in it before executing node or npm. Node.exe doesn't have to be in the project folder as long as it is in the path (by default it is)
Make sure your source files in that project folder. (test.js, introduction.js .. whatever you will run)

Nodejs webserver use script port 3000

I created a script in nodejs that extract data from database and create a file with all db data. I create also a webserver node js listening on port 3000 and with forever is working in listening mode all time. But now the script is without web interface and use a command line prompt and other modules. How remote users can use a remote script like : node scrip.js like a command shell prompt on locale machine ?
This code working but in-globe only hello world not my prompt command and db retrieve info. Thanks
var app = connect().use(connect.static('public')).listen(3000, "0.0.0.0");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');
I'm not completely sure I understand what you are asking. But if you are just asking how to communicate with a local server running on port 3000.
Then if this is a GET request, you could simply type 'http://localhost:3000/' in a browser, or you could use a tool like postman, I recommend the Chrome app.
Local server is listening on port 3000, but i can see only hello world. But when one user access to my url http://ip:3000/ needs to use a command shell like >
node script.js because i have different operations db retrieve information and input user data from command line input.

How to run a website developed with node.js in local?

I would like to run a website developed with node.js in local.
I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js )
Also, I guess I have to simulate a server ? How can I do that with node?
You can start a simple server with the example that can be found on nodejs.org:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
https://nodejs.org/en/about/
To develop a website it is very helpful to use a web framework such as Express.
http://expressjs.com/
You should use:
npm start file.js
but also be sure to check out nodemon, which is very helpful for debugging - it restarts your app on code change.
Also be sure to check out the express generator, which will set up a node+express app that you can check out to figure how to get the server and routes going.

How running Node.js files works?

With my current web development practices, I always run my code (html, css, javascript) on an internet browser.
I'm trying to learn Node.js, and all the tutorials are using command line to run the node.js files.
My problem is, I don't know how to use terminal for development purposes (I've never really used it at all)
Why would I even be running a website on a terminal window rather than a browser where it should be run?
And lastly, can I run node.js code on a web browser, similar to how I run my javascript files? Or do i HAVE TO run Node.js on a terminal window if I want to test it or something?
This content shows 3 basic steps:
Step 1 Install Node.js
Step 2 Use an editor to write some JavaScript
Step 3 Run the application "node appName.js" and a browser to test "host-ip:port" ex: http://127.0.0.1:3000
source code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

Categories

Resources