Trying to run node app on fedora server - node.js

Ok I am making website and want to use mongo, express, etc. I setup a server using fedora server ISO. The problem is getting node working. I have followed several tutorials, and its all the same. Nothing works. So I have to be doing something wrong. Trying to get the simplest thing to display on screen.
I think the server is running httpd server, whatever fedora has built in. I get the default fedora server page when going to the url. So the server is running and working, just hasn't been configured. When running node on the server do I have to use httpd-node? Or can it be http, etc.
Here is my app.js
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
And then I have a basic index.html that should be rendered just saying test.
I ssh into the server and run node start, it runs and the console logs the message like it should. But if I go to the address 192.168.1.5, or the domain that points to the server, I get nothing, just a blank page.
If someone can help me get this working, I can actually get to work coding out the application. Any help would be appreciated.

I think you make a confusion. When you build an Express application, you do not need another server at all.
When you start your app with:
app.listen(3000, function () {})
Express returns an http.Server object listening to port 3000.
When you navigate to your local adress on port 3000, you will see your "hello world" message.
It is possible that httpd service is already running in your Fedora environnement on default port 80 (the default port for http, the one your reach when you go to your local adress) but this is an Apache server and you do not need it to run your Nodejs app.
To build a Nodejs server, you can also use httpd-node package, but this is redundant as you're using Express framework.
If you need to serve a simple html file, a method I like for its simplicity is to use ejs template engine, something like this.

res.send('Hello World!') - this is your problem! Why?
How you receive this answer on client side?
Solution: use res.render(..) - for rendering from server or use AJAX on client side for receive this text!
P.S: render page and you don't see blank page anymore! Or use client-server conversation logic with your server through AJAX.
Try 192.168.1.5:3000
If I wrong: show your full project setup...
Test your app with curl (https://curl.haxx.se)! Check connection establishment, and show results here!

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

How to get Node.js http request to work from a browser on Siteground (shared hosting)?

Ok, I've searched around a bunch, and maybe the answer is here and I just couldn't find it because I wasn't using the right search combination or whatever, so please bear with me!
I'm very new to Node and all the tutorials I've read/watched (understandably) use localhost with a port number for examples. I'm having difficulty translating that into a real, shared-server website.
I successfully got Node.js installed on my shared hosting on Siteground (I can check versions, run javascript by pulling up node through putty, etc.).
I can start an http server using port 3000 and my ip address, and I can confirm that the server listener is working using another putty window and curl ipaddressORdomainname: 3000 (for testing purposes until I get it connected). I'm running the following to start the server:
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req.url, req.method);
});
server.listen(3000, 'my_ip_address', () => {
console.log('listening for requests on port 3000')
})
And running curl ipaddressORdomainname: 3000 in another putty window successfully displays / GET in the other putty window with the open port.
What I can't seem to do is use any web browser (tried latest versions of Brave, Chrome, Edge, Firefox, Opera to no avail) to "connect" and show the same result I get from the curl line.
I'm ultimately trying to connect a website to the node.js server, and I think this is my final missing piece I can't seem to find a solution to.
Any guidance would be very appreciated.
Thanks!

App.listen on an already running port of shared hosting

var express = require('express');
var app = express();
app.get("/", function(req,res){
console.log("running on that weird path i made///");
res.send("HELLO THIS IS A WEBPAGE TRYOUT!");
});
app.listen(3000, function(){
console.log("Server running on 3000");
})
I’m trying to create a simple app (for practice) which does a simple hello world. Using express via node.js
I can do it locally no problems.
The problem is I’m trying to run it on an existing server using cPanel shared hosting.
And there is obviously an already running domain name(and has website installed in Wordpress), so I cannot do a simple app.listen to a port which is obviously already running. And I’m guessing therefor I cannot do app.get(“/“....)
I’ve tried a different port-but the get request won’t work(I think obviously since that’s not the one running?)
So when I tell it to listen to the cPanel port, it throws an error that it’s already running.
And I don’t want to stop from the website to run. Is there a way to work this?
Tried also using subdomains. Same result.
Edit:
this worked on postman, when I've sent a get request to that address. tried port 3000, and it showed my res.send on the postman tab.
Edit2:
I've solved this...i forgot to put :3000 on the address..
as in www.example.com:3000
this doesn't work if i don't add the :3000.
and also if i do process.env.PORT and .IP..guessing its because its a shared hosting, or because wordpress is installed...
I won't delete just in case anyone did the same mistake as me.
If the problem that the port is already in use, you may try to use 0 as a port that will cause it to take a random free port on the machine.
const server = app.listen(0, function(){
console.log(`Server is listening on http://localhost:${server.address().port}`);
})

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 :)

How to deploy a socket.io node.js application on windows azure?

I am building windows azure application which is primarily based on .NET, but I also have to build a socket.io server using node.js hence i need to deploy a socket.io server and use this socket.io url to connect in my .NET application.
I followed all the steps listed here . And I am able to get the socket.io running on my local but when i deploy to cloud, it doesnt start. Please find below a code snippet for socket.io
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server, { origins: '*:*' });
server.listen(4001);
When i hosted it in my local emulator, 127.0.0.1:81 was pointing to this in my browser
But 127.0.0.1:4001 showed "Cannot GET /" on the browser, which is an indicative that the socket.io server is running on that url.
But when i deploy the same to cloud, i get the same as the screenshot on the url where the cloud service is hosted but on port 4001 where the socket.io server should have started it says page cannot be displayed.
Please let me know if you need to see any other files like web.config etc.
I have been stuck on this issue from forever and its really crucial for my project, any suggestions or ideas would be deeply appreciated.
Thanks
The important part that you are missing from the sample is setting of the port number
var port = process.env.port || 1337;
and
.listen(port)
when you are running inside of the Azure environment (even emulated) the ports are assigned for you, the port environment variable will tell you where. 4001 is likely not the assigned port.
The 1337 would only be used if you are running by executing
node server.js
from the command line

Resources