Postgresql on OpenShift 2, using node.js - javascript

I have an app that uses Node.js and Postgresql on OpenShift, I can connect locally to the database and make queries, but I can't get it to work on the openshift server. When I push to server, I get this error:
Waiting for application port (8080) become available ...
Application 'myapp' failed to start (port 8080 not available)
But Im using the port 8080...
My openshift ports are:
Service --- Local --------------- OpenShift
node ------ 127.0.0.1:8080 => 127.8.120.129:8080
postgresql 127.0.0.1:5432 => 127.8.120.130:5432
And here I write the important code line.
First, the server.js:
...
var db = require('./postgresql/database.js');
db.sync();
...
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
server.listen(server_port, server_ip_address, function () {});
...
And database.js:
var Sequelize = require('sequelize');
var bd_url = process.env.OPENSHIFT_POSTGRESQL_DB_URL || 'postgres://'user':'pass'#127.0.0.1:5432/sw'
var sequelize = new Sequelize(bd_url, {
dialect: 'postgres',
dialectOptions: {}
});
module.exports = sequelize;
Does anyone know what can fail?
Thanks!

OpenShift provides a default web server (written in Ruby) on almost every container/cartridge you create.
Every service is started using the "start" service hook, located at:
$OPENSHIFT_REPO_DIR/.openshift/action_hooks/start
You may find a line like this one:
[]\> nohup $OPENSHIFT_REPO_DIR/diy/testrubyserver.rb $OPENSHIFT_DIY_IP $OPENSHIFT_REPO_DIR/diy |& /usr/bin/logshifter -tag diy &
In order to verify which application is using port 8080, you can execute "oo-lists-ports" command.
This command is just an alias for "lsof" command.
Execute it without any arguments and you'll obtain the application that it's locking your port 8080 (in my case):
[]\> oo-lists-ports
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 88451 1027 10u IPv4 392176 0t0 TCP 127.2.1.129:8080 (LISTEN)
[]\>
With the above information (PID), you just need to kill the related process:
(in my case)
[]\> ps -ef |grep 88451
1027 62829 61960 0 08:33 pts/0 00:00:00 grep 88451
1027 88451 1 0 Jun21 ? 00:00:16 node faceBot.js
[]\> kill -9 88451
After killing the process that is locking your port 8080 you will be able to run your Node JS stack on that port.
Regards

Related

Docker compose, JS: Issue while connecting to doc. container PHP Apache with socket.io from localhost

Windows 10, Docker Desktop + VS;
Docker-compose.yml has images:
node.js with socket.io
php 7.4-apache with socket.io
I need to connect socket.io within php-apache (website) to socket.io within node.js (server to handle data from php-apache) in one docker-compose.yml to run it in remote VM (hah, sounds like it is possible for an ordinary mortal).
First, tried to connect 2 containers within docker-compose (node.js to node.js) to be sure docker ports + socker.io ports set correct (successfully).
(Server side: socket.io listen 90 port, docker 90:90 (service name in docker-compose is node_server) || Client side: io(http://node_server:90), docker 85:85).
Confirmed, 2 containers are linked and ports are set correctly, started to make "php apache - node.js" socket-io docker-compose.yml.
My php-apache container with inner socket.io client composer link is "http://node_client:80".
When tried to connect "localhost:80" to PHP container: everything loaded fine, but error occured: net:ERR_NAME_NOT_RESOLVED (polling-xhr.js:206);
Error which I get while connecting to localhost:80 (docker php-apache container)
docker php-apache container is on 80:80 port in docker-compose.yml
request URL
I can connect to apache (it opens windows and all html stuff written in index.php), but gets an error (err_name_not_resolved) like socket.io in client-side (php-apache) can't make a request(?).
Checked for ping: both have connection (pinged node_server:90\client:80 from each terminal).
Checked "php-apache docker": "curl "http://node_server:90/socket.io/?EIO=4&transport=polling" and it also showed an information.
I do understand that some troubles have to be occured (because I connect from localhost to docker php container, and socket.io client in that container in that container gets mad(what ip to use(172.0.. or 192.168.. etc). But I have even no idea how to solve it (I need to connect somehow to index.php apache and socket.io.
I need to use php-apache and connect it to node.js, I confirmed socket.io worked in node.js - node.js, but in php-apache->node.js something happens while connection localhost.
docker-compose.yml:
version: "3"
services:
node_client:
container_name: client
image: img_client
ports:
- "80:80"
networks:
- test
node_server:
container_name: server
image: img_server
ports:
- "90:90"
networks:
- test
networks:
test:
external: true
Docker.client:
FROM php:7.4-apache
COPY ./client /var/www/html
EXPOSE 80
#(in ./client -> index.php)
./client/index.php:
<script src="https://cdn.socket.io/3.1.3/socket.io.min.js" integrity="sha..." crossorigin="anonymous"></script>
<script>
const socket = io(`http://node_server:90`, {
//secure: true,
//transport: ['websocket'],
//upgrade: false,
//rejectUnauthorized: false,
});
socket.on('connect',() =>{console.log(socket.id)});
</script>
Docker.server:
FROM node:alpine
COPY ./server /app
WORKDIR /app
COPY package*.json ./
COPY . .
CMD [ "node", "./server.mjs" ]
EXPOSE 90
//(in ./server -> server.mjs + node_modules)
./server/server.mjs:
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const express = require('express')
const app = express();
const cors = require("cors")
//app.use(cors({}))
const server = require('http').createServer(app);
const { Server } = require("socket.io");
const io = new Server(server, {
//rejectUnauthorized: false,
cors: {
origin: '*',
//methods: ["GET", "POST"],
//credentials: true,
}
});
server.listen(90, () => { console.log("Server is Ready!");});
//Also tried (90, '0.0.0.0', () => but there will be CORS troubles i believe, anyway I can't even determine the problem, but socket.io in "node.js + node.js" worked
//server.listen(PORT_SERVER, '0.0.0.0', () => { console.log("Server is Ready!");});
io.on('connection', (socket) => { console.log(`Подключился!`)});
Heh.. I tried to understand what is proxy and can it be used (nginx) somehow to connect 1th docker php-apache with CDN module (client) + 2th docker container node.js (server) all within docker-compose.yml, but gave up.

Node deploy failing because container did not respond to warmup request

I'm deploying a node app to azure and the deploy is failing because the container is not responding to the warmup request. The app starts locally, and is listening on the same port as the warmup requests. Here are the deploy logs:
2022-10-29T06:01:45.287Z INFO - Initiating warmup request to container
customcalligraphy_0_9c31f200 for site customcalligraphy
2022-10-29T06:03:45 No new trace in the past 1 min(s).
2022-10-29T06:04:45 No new trace in the past 2 min(s).
2022-10-29T06:05:45 No new trace in the past 3 min(s).
2022-10-29T06:01:45.035657849Z _____
2022-10-29T06:01:45.035686150Z / _ \ __________ _________ ____
2022-10-29T06:01:45.035691350Z / /_\ \\___ / | \_ __ \_/ __ \
2022-10-29T06:01:45.035695650Z / | \/ /| | /| | \/\ ___/
2022-10-29T06:01:45.035699650Z \____|__ /_____ \____/ |__| \___ >
2022-10-29T06:01:45.035703750Z \/ \/ \/
2022-10-29T06:01:45.035707650Z A P P S E R V I C E O N L I N U X
2022-10-29T06:01:45.035711351Z
2022-10-29T06:01:45.035714951Z Documentation: http://aka.ms/webapp-linux
2022-10-29T06:01:45.035718651Z NodeJS quickstart: https://aka.ms/node-qs
2022-10-29T06:01:45.035722451Z NodeJS Version : v18.2.0
2022-10-29T06:01:45.035726151Z Note: Any data outside '/home' is not persisted
2022-10-29T06:01:45.035729951Z
2022-10-29T06:01:45.152855802Z Starting OpenBSD Secure Shell server: sshd.
2022-10-29T06:01:45.215494733Z Starting periodic command scheduler: cron.
2022-10-29T06:01:45.235215447Z Cound not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
2022-10-29T06:01:45.242910847Z Could not find operation ID in manifest. Generating an operation id...
2022-10-29T06:01:45.242928347Z Build Operation ID: feba110f-d139-44e2-b66c-0e10cff5c53d
2022-10-29T06:01:45.476505931Z Environment Variables for Application Insight's IPA Codeless Configuration exists..
2022-10-29T06:01:45.522707434Z Writing output script to '/opt/startup/startup.sh'
2022-10-29T06:01:45.595152421Z Running #!/bin/sh
2022-10-29T06:01:45.595192122Z
2022-10-29T06:01:45.595198422Z # Enter the source directory to make sure the script runs where the user expects
2022-10-29T06:01:45.595204922Z cd "/home/site/wwwroot"
2022-10-29T06:01:45.595210723Z
2022-10-29T06:01:45.595215423Z export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
2022-10-29T06:01:45.595220223Z if [ -z "$PORT" ]; then
2022-10-29T06:01:45.595224923Z export PORT=8080
2022-10-29T06:01:45.595229723Z fi
2022-10-29T06:01:45.595234423Z
2022-10-29T06:01:45.595239023Z npm start
2022-10-29T06:01:47.149225225Z npm info it worked if it ends with ok
2022-10-29T06:01:47.149401829Z npm info using npm#6.14.15
2022-10-29T06:01:47.150028044Z npm info using node#v18.2.0
2022-10-29T06:01:47.346984068Z npm info lifecycle custom-calligraphy-ecommerce#1.0.0~prestart: custom-calligraphy-ecommerce#1.0.0
2022-10-29T06:01:47.348375300Z npm info lifecycle custom-calligraphy-ecommerce#1.0.0~start: custom-calligraphy-ecommerce#1.0.0
2022-10-29T06:01:47.352639898Z
2022-10-29T06:01:47.352662299Z > custom-calligraphy-ecommerce#1.0.0 start /home/site/wwwroot
2022-10-29T06:01:47.352681799Z > node index.js
2022-10-29T06:01:47.352686199Z
2022-10-29T06:01:48.466127815Z STARTING CUSTOM CALLIGRAPHY SERVER
2022-10-29T06:01:49.692607132Z CCvbeta1.1 Listening on port 8080!
2022-10-29T06:01:50.344268374Z serving app
2022-10-29T06:02:21.332686762Z serving app
2022-10-29T06:02:52.341610061Z serving app
2022-10-29T06:03:23.343535160Z serving app
2022-10-29T06:03:54.350896814Z serving app
2022-10-29T06:04:25.361345562Z serving app
2022-10-29T06:04:56.364076228Z serving app
2022-10-29T06:05:27.370947666Z serving app
2022-10-29T06:05:57.383Z ERROR - Container customcalligraphy_0_9c31f200 for site customcalligraphy did not start within expected time limit. Elapsed time = 252.0962469 sec
2022-10-29T06:05:57.401Z ERROR - Container customcalligraphy_0_9c31f200 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2022-10-29T06:05:57.408Z INFO - Stopping site customcalligraphy because it failed during startup.
My app is actually running, but the deploy is failing. Here is my serverside code:
console.log("STARTING CUSTOM CALLIGRAPHY SERVER");
const express = require("express");
const path = require("path");
const app = express();
app.use(express.static(path.join(__dirname, "frontend", "build")), () =>
console.log("serving app")
);
app.use(express.json({ limit: "5gb" }));
const port = process.env.port || 8080;
const version = "beta1.1";
app.listen(port, () => console.log(`CCv${version} Listening on port ${port}!`));
How can I disable the warmup request or respond to the pings? Any help would be appreciated.
I would suggest use of express-generator to create the app. Just run
npx express-generator myapp
Then you can add your code in the app.js file.
my app file looks like this
console.log("STARTING CUSTOM CALLIGRAPHY SERVER");
const express = require("express");
const path = require("path");
const app = express();
app.use(express.static(path.join(__dirname, "frontend", "build")), () =>
console.log("serving app")
);
app.use(express.json({ limit: "5gb" }));
const port = process.env.port || 8080;
module.exports = app;
Here instead of listening to the ports in the app.js file we export the app entity and all the listening of port will be done in the www file.
This www also call the app.js file and will run app.
Now you can just deploy the app by you preferred way of choice. Here I have used vscode.
This way you will not get the could not find javascript build manifest file errors
logs after deployment of app :
Also use port 80 as it is already exposed in the app service.

how to get the port number from the service name using nodejs

We are developing a WebSocket server using Nodejs with socket.io
I have used createServer method from class HTTP and then listen to a port as follows :
var server = http.createServer();
server.listen(port, function () {
...
}
Now The problem is that I only have the service name
and listening to a service doesn't work :
var server = http.createServer();
server.listen(service, function () {
...
}
So I need to read and parse the file /etc/services to get the port associated with the service as follows :
var str = fs.readFileSync(filename).toString();
var serviceline = str.match( port+".*tcp" );
var port = serviceline[0].match( "[0-9]+" )
Is there a simpler way to get the port from the service?
Thanks in advance
Jean-Marie
You can use find-process to find port from process or vice-versa
Command line usage
npm install find-process -g
Usage: find-process [options] <keyword>
Options:
-V, --version output the version number
-t, --type <type> find process by keyword type (pid|port|name)
-p, --port find process by port
-h, --help output usage information
Examples:
$ find-process node # find by name "node"
$ find-process 111 # find by pid "111"
$ find-process -p 80 # find by port "80"
$ find-process -t port 80 # find by port "80"
Code usage
npm install find-process --save
const find = require('find-process');
find('pid', 12345)
.then(function (list) {
console.log(list);
}, function (err) {
console.log(err.stack || err);
})
I have used this to find process from the port but I think it provides a reverse way to or you can skim down to its repo and pick the code you needed.
I don't need a server port but the port corresponding to a service name
for instance having the following service "wsgw" specified in file /etc/services
wsgw 8001/tcp # websocket gateway
I need to get the value 8001 before running the server listening to this port this way :
var server = http.createServer();
server.listen(8001, function () {
...
listening to a service as below doesn't work :
var server = http.createServer();
server.listen("wsgw", function () {
...
note : finding a port from a process doesn't help me as it is not a process but a service

Can't access to socket.io.js on Raspberry Pi with Lighttpd [Node.JS & Socket.IO]

I'm completely new to Node.JS and Socket.IO since yesterday.
I try to make Node.JS and Socket.IO work on my Raspberry Pi but it doesn't seem to. I can't access to <myip>:1337/socket.io/socket.io.js.
I have followed this tutorial so my Lighttpd.conf file seems like so:
$HTTP["host"] == "<myURLtomywebsite>" {
proxy.server = (" " => ((
"host" => "<myIP>",
"port" => 1337)
)
)
My server.js look like so:
var http = require('http');
httpServer = http.createServer(function(req, res) {
res.end('Hello World!');
});
httpServer.listen(1337);
var io = require('socket.io').listen(httpServer);
var clients = 0;
io.sockets.on('connection', function(socket) {
++clients;
socket.on('disconnect', function(data) {
--clients;
io.sockets.emit('disusr', clients);
});
io.sockets.emit('newusr', clients);
});
And I bind to the disusr and newusr events in my client.js to display the number of connected users in a div.
Everything looks fine on my localhost but, in production environment, I cannot link to my socket.io.js file on the 1337 port. To be honest, I'm not even sure what address to use? (URL of my website appended with :1337, localhost, some other address I would have created?)
Any help would be much appreciated. Thanks!
I resolved my problem!
I linked socket.io.js like so : <script type="text/javascript" src="/socket.io/socket.io.js"></script>
I used HAProxy instead of Lighttpd mod_proxy as specified in this question
Here is my conf file (amend <...> per your configuration):
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
uid 99
gid 99
daemon
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option http-use-proxy-header
option redispatch
option http-server-close
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend public
bind *:80
acl is_example hdr_end(host) -i <URL.toyourwebsite.com>
acl is_websocket hdr(Upgrade) -i WebSocket
acl is_websocket path_beg -i /websockets
use_backend ws if is_websocket is_example
default_backend www
backend ws
balance roundrobin
option forwardfor # This sets X-Forwarded-For
timeout queue 5000
timeout server 86400000
timeout connect 86400000
server apiserver localhost:<PORT> weight 1 maxconn 1024 check
And I made Lighttpd listened to the 8080 port (otherwise HAProxy wouldn't start).
Remind there is no need to use mod_proxy as it is known to be not compatible with websockets. Use HAProxy instead.

Using MySQL on SilkJS Under OSX Lion

I'd like to use MySQL from SilkJS under OSX Lion, but can't find the exact steps to get it to work. I followed the general instructions found here:
Perform the SilkJS OSX Lion Quick Install
Install MySQL
Load MySQL from SilkJS REPL
Download/configure:
$ curl http://silkjs.org/install-osx.sh | sh
$ sudo port install mysql5-server +mysql5
$ sudo -u _mysql mysql_install_db5
$ sudo port load mysql5-server
Starting the SilkJS REPL, I ran:
$ silkjs
SilkJS> var MySQL = require('MySQL');
undefined
SilkJS> var SQL = new MySQL();
undefined
SilkJS> SQL.connect();
interpreter line 19 (main):
(object) :
SilkJS> SQL.startTransaction();
Caught Signal 11 for process: 77312
How can I run a simple MySQL query from SilkJS under Lion?
Fresh web install of silkjs on OSX Lion:
mschwartz#dionysus:~/src$ curl http://silkjs.org/install-osx.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1666 100 1666 0 0 3553 0 --:--:-- --:--:-- --:--:-- 79333
Installing SilkJS in /usr/local/silkjs
You may be asked to enter your password.
This is so the /usr/local directory can be made writable for the install.
Password:
Downloading SilkJS
######################################################################## 100.0%
Installation complete.
You need to add /usr/local/bin to your PATH environment variable.
This can be done by adding this line to your .bashrc file:
export PATH=/usr/local/bin:$PATH
You can run SilkJS with the following command:
$ silkjs
You can run the SilkJS HTTP Server with the following command:
$ httpd-silk.js yourapp.js
For instructions on setting up a WWW site for httpd-silk.js, see
http://silkjs.org/http-server/
Now to see it works:
mschwartz#dionysus:~/src$ silkjs
SilkJS> console.dir(require('MySQL'));
undefined line 1 (eval):
function () {
this.queryCount = 0;
this.handle = null;
}
undefined
SilkJS>
Note the console.dir() shows a function. The rest of the methods defined as properties of the function (constructor). So you must construct a MySQL() object:
SilkJS> var mysql = require('MySQL');
SilkJS> m = new mysql();
SilkJS> console.log(m.connect)
function (host, user, passwd, db) {
if (!this.handle) {
host = host || Config.mysql.host;
user = user || Config.mysql.user;
passwd = passwd !== undefined ? passwd : Config.mysql.passwd;
db = db || Config.mysql.db;
this.handle = mysql.connect(host, user, passwd, db);
}
}
undefined
SilkJS>
Now, in order to connect, you have to pass in host, user, passwd, and db. Or if you're running in the httpd environment, you set Config.mysql, something like this:
Config.mysql = {
host: 'localhost', // or some other host running MySQL server
user: 'someuser', // username to authenticate in MySQL server
passwd: 'whatever', // password of username in MySQL server
db: 'database_name" // name of database in MySQL server to connect to
};
Then the HTTP server will create a global SQL object available from request to request. This is automatic, you can just use SQL.getDataRows(), SQL.getScalar(), and so on.

Categories