Output in browser using Express Nodejs - node.js

I install nodejs with express in server (subdirectory in ftp,path is /var/www/html/admin)
and in admin folder i created file "app.js" which is working in xshell fine and showing message in console
here is my app.js
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
but now i want to show result in browser,how can i do this
i tried with following urls but not working for me
http://myurl.com/admin:3000
http://myurl.com/3000/admin

First get ip address of your FTP server.
Then connect http://<FTP_SERVER_URL:3000 to see 'Hello World' you wrote in the code.
To browse /admin folder you mentioned and if your express app is running on root directory, try http://<FTP_SERVER_URL:3000/var/www/html/admin

You can use document.write() method to write something to the DOM object( or in layman's language you can call it browser screen).
Try using the following:
app.listen(port, () => document.write(`Example app listening on port ${port}!`))

Related

Node Js hello world Program is not being displayed in localhost

const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
enter image description here CMD
enter image description here local host
I need to run node js successfully and see the hello world output on local host
That code seems to be OK.
Please try http://127.0.0.1:3000
Or try to move your project to different folder, I see it is placed in One Drive folder ? Maybe some permission issue ?
I hope you also tried restart your machine if you just installed NodeJS before trying this out.

Trouble setting up simple local host using nodejs with npm package express, any reason why program will not run

This is the method i followed, installed express from npm. Create new folder in cmd using mkdir, go to that directory open in vs code, create file called index.js
used the following code:
const app = express();
app.get('/', (req, res) => {
res.send('Hello World')
});
app.listen(3000, () => console.log('Listening on port 3000...')
when running the file from cmd using
node index.js
nothing happens not even an error code, any help would be appriciated

Cannot access Nodejs/Express Server on macOS High Sierra from other computer

I started an simple express server on MacOS High Sierra. localhost:3000 and 127.0.0.1:3000 work fine. However, when I accessed the server via http://192.168.x.x:3000 from another computer, I didn't get any response (ERR_EMPTY_RESPONSE).
I tested the server on another MacBook (MacOS Sierra) and a Windows computer, everything works fine.
So I suppose there are something wrong with MacOS High Sierra.
Any help would be welcome. Thanks in advance.
Here is the code :
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
UPDATE MacOS
I have tried to reinstall node, npm, change share settings, but they don't work, neither.
Finally, I updated MacOS (10.13.4 --> 10.13.5) and now other computers can access my server like a charm.
----------Update -------------
After a few (happy) day, the issue raised again.
Fortunately , now I pretty sure that ESET EndPoint Security was the problem since it's the last application I installed.
So I open the ESET firewall and create a new rule which allow local network address to connect via TCP&UDP.
The server once again works like charm.
Hope this help the other.
Refer to https://nodejs.org/en/docs/guides/getting-started-guide/
Once you have installed Node, let's try building our first web server. Create a file named "app.js", and paste the following code:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
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}/`);
});
After that, run your web server using node app.js, visit http://localhost:3000, and you will see a message 'Hello World'

Howto run google assistand bitcoinInfo example webhook index.js

I am trying to run the google assistant example webhook nodejs application(index.js) on my own server, but don't know what is the webhosting setup for this..
https://codelabs.developers.google.com/codelabs/your-first-action-on-google-with-webhook/#2
What is the environment to run this app on my server? Since it's not running as a listening server, I can't use nginx, node_cgi is not mature with apache, how am I supposed to run this sample?
Excellent point, and you should be sure to file a bug request on the page to indicate it is unclear.
The code, as presented, is meant to run using Google Cloud Functions.
This doesn't mean you can't run it on your own server - just that you need to know how to run a Node.js server outside of your Apache or Nginx environment. I've seen a number of configurations, but typically you'll have the Node.js server application running and listening to a local port and have a proxy between your externally facing web server at a particular path and this port.
But even that isn't sufficient in this case - the code itself doesn't listen on a port - it expects to be handed a request and response object in the form that Express.js with a JSON middleware can handle. To do that, you'll need to have installed the Express.js library and then start listening with code such as:
const express = require('express');
const app = express();
app.use( express.json() );
app.get('/', (req, res) => exports.bitcoinInfo( req, res ));
app.listen(3000, () => console.log('App listening on port 3000!'));
Thanks for the help to #Prisoner and #Ido Green link works even better! The minimum to run the sample I did the following:
Create a new nodejs project with mainfile main.js, install express and actions-on-google
mkdir googleActionServer
cd googleActionServer
npm init
npm install --save actions-on-google
npm install --save express`
Copy the index.js from google and put this into main.js
const express = require('express');
const bitcoinInfo = require("./index");
const app = express();
app.use( express.json() );
app.post('/', (req, res) => bitcoinInfo.bitcoinInfo( req, res ));
app.listen(3000, () => console.log('App listening on port 3000!'));
Start the application by running:
node ./main.js
To test with DialogFlow, download and install ngrok to /usr/local/bin for ex and then run:
ngrok http 3000
Ngrok will give you an url that is accesible from outside and forward the requests to the nodejs app. It will also create a https for you, so copy paste the https address into DialogFlow webhook address and you are set to go

NodeJS Deployment confusion between localhost and other domain?

I have a simple program which executes fine in localhost.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var port = process.env.PORT || 8080;
var host = "127.0.0.1";
var server = app.listen(port, host, function(){
console.log("Server running in : ",host ," with port no : ",port);
});
Trying to deploy the same to heroku using codeship. Everything is building perfect except the last line of deployment test command i.e node index.js which in turn is referring to 127.0.0.1 and stops deploying. May i know do i need to change something here for the host and port address
Just don't provide a host:
var server = app.listen(port, function() {
console.log('Server listening on', port);
});
(This implies, "accept connections on any host, on this port", vs what you're trying which implies, "accept connections on 127.0.0.1 on this port")
Try to run your app on localhost with the help of foreman that is a part of the Heroku Toolbelt. For instance:
foreman start web
You should see your app running on http://localhost:5000 or the port you have specified in your package.json file.
Suggest this link for further queries:
prerequisites to deploy a node app on Heroku?
I was able to host it successfully following through this steps
As suggested by #hunterloftis, i removed hostname.
More importantly, Procfile was missing,so added it and deployed successfully

Resources