node.js script works off command line but not from browser? - javascript

I launched and amazon ec2 instance running ubuntu 12.04
I then followed the instructions to install node.js from here http://howtonode.org/how-to-install-nodejs
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install
I then used the example code to make hello.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
I then ran hello.js
/var/www$ node hello.js
Server running at http://127.0.0.1:8124/
However, when I try to access this from the url, with http://ec2-***.compute-1.amazonaws.com:8124/ I get an error page from my browser.
Any advice on how to get it to show up in the browser?
EDIT
I'm still encountering this problem after changing the above line of code
}).listen(8124, "127.0.0.1");
to this
}).listen(8124);

127.0.0.1 is the loopback address. Only the host can access it. If you want to listen on any available IP, just don't specify that parameter. Otherwise, specify the real IP you want to listen on.

I also had same issue, Uninstalling skype worked for me, You can also search for some setting which force skype to move to some other port.

Related

typescript, javascript, angular, nginx, alpine, docker communications in the network via nginx i think i missed something. looking for a review

once i migrated to docker to have a virtual network to simulate an atual network (bridge type with dns which works . the fqdn is resolved correctly to referrring ip) the following errors appeared in the console.log AND no data is displayed on the frontend website.
ERROR Error: NG0901
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://backend:4000/crafts. (Reason: CORS request did not succeed). Status code: (null).
ERROR
Object { headers: {…}, status: 0, statusText: "Unknown Error", url: "http://backend:4000/crafts", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://backend:4000/crafts: 0 Unknown Error", error: error }
thats the browser's (firefox) console.log
i think nginx is doing things with the headers and or the body is empty due to serversides configs with nginx
on local host everything worked out fine
so im on the config of gninx but so far without any success.. i read about similar problems but couldnt find a solution myself OR the answers read didnt work with my setup.
i tries to change the ip to 0.0.0.0 to make it accessable in the network
oh AND im using nodejs expressjs
app.listen(port,ip)
I use a Dockerfile and docker-compose.yml to make the images, i use a powershell script to compose the images
what i suspect to cause the problem is:
backend:
index.js is run anbd looks like that
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const Routes_1 = __importDefault(require("./Routes"));
const app = (0, express_1.default)();
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
next();
});
// middleswares
app.use(express_1.default.json());
app.use(express_1.default.urlencoded({ extended: false })); //changed to see wheater it would effect the package isssue- should allow
app.use(Routes_1.default);
app.listen(4000,'0.0.0.0'); // or fqdn 'frontend'
console.log('server on port', 4000);
this is generated from index.ts and a build command
the referrring dockerfile:
FROM node:alpine as builder
WORKDIR /app/
COPY . /app/
COPY package.json /app/
COPY package-lock.json /app/
RUN cd /app/
RUN npm install -g
RUN npm update express
RUN npm install pg
FROM nginx:alpine
COPY --from=builder ./app/dist ./usr/share/nginx/html/
EXPOSE 3999-6001
CMD ["nginx", "-g", "daemon off;"]
RUN apk add --update nodejs
RUN apk add --update npm
after the image runs i open the terminal and run in the usr/share/gninx/html directory :
npm i express
npm i pg
node index.js
then I install vim
and edit the nginx.config like that
vi /etc/nginx/nginx.conf
i add a server directory, make it listen to the fqdn 'frontend' or its referring IP and the port 4000
listen ip:port kind of syntax
i add error and access logs earlier on and it doesn't return problems besides sometimes it says that IP are not available. im lacking on the understanding on how to interpret that
the PostgreSQL is also running in a docker container by the default port 5432 and the fqdn database which is also properly resolvable
same as the backend's fqdn
there is so much more stuff that links the short pieces of code that i have.. feel free to request more if interested or if u think it'd be required to find out whats going wrong.
I learnt my lesson..
servers listen to their own IPs, or at their localhost.
so i had a misconception there. though thanks to the pple taking a look inside here.
also a nodejs expressjs server doesn't necessarily need nginx to run on.. node is enough.. for the purpose..
fixing these two things led functionality as designed :)
so this can be closed or used as reminder on these two things:
understanding the conceptional idea of how networks work
AND
understanding the tech-stack being used and how it works
else
/closed

In VPS with OS Ubuntu 18.04 "apt install nginx" shows libc6 & libcrypt1 error. So, how to install libc6 & libcrypt1 in it?

I am trying to host node app on vps using nginx server. But I am unable to install nginx in ubuntu 18.04 version after apt install nginx shows:
The following packages have unmet dependencies:
nginx : Depends: libc6 (>= 2.28) but 2.27-3ubuntu1.6 is to be installed
Depends: libcrypt1 (>= 1:4.1.0) but it is not installable
E: Unable to correct problems, you have held broken packages.
Anyone face such problem & how do you solve it? I also got problem in nginx.cnf, after nginx -t to check it shows brotil error.
ubuntu nginx installation
node app
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}`)
})
I want to host node app to serve backend API's. So, I bought VPS from Hostinger website & have installed Ubuntu v18.04 in it. Then I successfully installed nodejs to run nodeapp & pm2 to manage processes. It was successfully running on port 8000. But problem is when I called the API from frontend, shows SSL security error.
So, for that I tried to install nginx but shows error & I guess for SSL nginx server is required. Now I am stuck in nginx installation.
I need some solution from someone who also face same problem.
You should consider upgrading to more recent versions of ubuntu as version 18.04 will reach EOL on April 30, 2023
Otherwise, you may try upgrading packages on your system, as it currently has libc6 version 2.27 while version 2.28 or higher is required.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nginx
if you continue seeing an issue, you may try
sudo apt-get install --no-install-recommends nginx
Actually, I had installed Ubuntu v18.04 with plex control panel. I guess that is giving me the issue.
Now I installed Ubuntu v18.04 only. It is working fine now. I successfully installed nginx and it is running properly.

How can i solve refresh problem in node js

I am a very beginner at node js programming language. I just want to asked that why i have to run on command line after each change in js file.
for example:
i changed form this
var http = require('http');
http.createServer(function (req, res) {
res.write("The date and time are currently displaying");
res.end('Hello World!');
}).listen(9001);
to
var http = require('http');
http.createServer(function (req, res) {
res.write("The date");
res.end('Hello World!');
}).listen(9001);
and when i refresh it did not change in browser.
And when i run it through cmd and then refresh the browser it changed.
Kindly tell me what should i do to instant change in browser whenever i
change in js file.And why it does't changed.
You might be running your node server using
node filename.js
If you want to just restart the server after every change made, you should use the nodemon package of npm.
Do:
Step1: Install nodemon using
(sudo) npm install -g nodemon
Step 2: After installation, just go to project folder and run
nodemon
Now on any change made to the node file, the node server will restart automatically with the latest changes.

Cannot access React app from production server

I have a react app which I can run on localhost without any problems. I dockerized it and put it on the production server, and sudo docker ps shows that the container is running. I used sudo docker run --name "name" -d -p "my port":my port" "uri"
to run the container, and I made sure to specify the correct port. But when I try to access the app from my browser by putting in the ip of the server and the port, I cannot access it. I am able to access other apps running on this server.
Any ideas as to what I'm doing wrong?
Just verify the return of your container, when you run it. Try:
docker run --attach STDERR ****
The command in my start script setting the port to be different than the default 3000 was not working in the docker container, although it worked when I ran the program on localhost. So instead, in my run command, I mapped port 3000 to the port I wanted to use on my server.

nodejs hello world example - symbol lookup error

UPDATE -- LINUX FEDORA 15
Following an example from:
http://simonwillison.net/2009/Nov/23/node/
My code:
var util = require('util'),
http = require('http');
http.createServer(function(req, res) {
res.sendHeader(200, {'Content-Type': 'text/html' });
res.sendBody('<h1>Hello World</h1>');
res.finish();
}).listen(8080);
util.puts('Server running at http://127.0.0.1:8080');
Produces the following error:
[abu#Beelzebub node_projects]$ nodejs helloworld.js
Server running at http://127.0.0.1:8080
nodejs: symbol lookup error: nodejs: undefined symbol: _ZN2v82V816IdleNotificationEv
this is 2009 tutorial and old api. You should do it like this
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
server.listen(8000);
console.log("Server running at http://127.0.0.1:8000/");
Your tutorial is old :) switch to this ->
http://howtonode.org/hello-node
To execute a node.js application, call it using node, not nodejs.
node helloworld.js
The particular error seems similar to a V8 build mismatch problem that was in Node 0.6.15. Have you tried using a newer (or rolling back to an older) version of Node?
To perform node.js installation on Fedora Linux download and install the standalone rpm (http://nodejs.tchol.org/stable/f16/SRPMS/repoview/nodejs.html) and perform install as follows:
Remove any existing node and nodejs applications using your package manager
Install node.js from standalone rpm
rpm –ivh
./configure
make
make install
Attempting to use a package manager may lead to dependency issues as described on the following site:
http://nodejs.tchol.org/

Categories

Resources