Full Stack: Http failure response for url: 0 Unknown Error - node.js

I am trying to deploy my full stack application built using MySQL, Express, Angular and Node on Plesk Obsidian. However, I am facing an issue during the deployment. When my client side sends a request to the server then, I get the following error:
"Http failure response for https://example.com:3000/api/mytargetapi: 0 Unknown Error"
"GET ... net::ERR_CONNECTION_REFUSED"
Everything was working smoothly until I deployed it on Plesk.
Here's my code of the request on the client side:
const params = new HttpParams().set('id', this.searchInDirectory);
this.http.get('https://example.com:3000/api/mytargetapi', {params})
.subscribe(response =>
{
this.itemDisplay = response;
}
server side:
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(cors());
app.use(routes);
const port = process.env.PORT || 3000;
const host = 'localhost';
const server = http.createServer(app);
server.listen(port, host, () => {
console.log(`Running on port ${port}`);
});
router.get('/api/mytargetapi', (req, res) => {
stuff here...
}
Does anybody know what am I doing wrong? I have searched the same question on stack overflow and followed each and every individual's solution but nothing seems to be working for me. Any help would be appreciated. Thanks!

I don't know how you're organizing your project structure for the back-end API, but you should test the API via POSTMAN before integration with your front-end client
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(cors());
app.get('/api/mytargetapi', (req, res) => {
stuff here...
}
app.use(routes);
const port = process.env.PORT || 3000;
const host = 'localhost';
const server = http.createServer(app);
server.listen(port, host, () => {
console.log(`Running on port ${port}`);
});

Related

Node.js (Express) Restful API App in Cpanel not working(APIs)? getting 503 Service Unavailable

In this case I have mention auth API, other APIs also not working getting same results
In localhost it's working(server.use('/auth', routeAuth);)
In server once I created Node app in Cpanel(server.use('/server/auth', routeAuth);) which is not working
In server code block below server.js
const express = require('express');
const server = express();
const cors = require('cors');
const routeAuth = require('./auth/auth');
const routeRegister = require('./auth/register');
const routeArticle = require('./api/article/article');
const PORT = process.env.PORT || 4300;
server.use(express.json());
server.use(express.urlencoded({ extended: false}));
server.use(cors());
server.use('/server/auth', routeAuth);
server.use('/server/', routeRegister);
server.use('/server/api', routeArticle);
server.listen(PORT, () => console.log(`Server started on port: ${PORT}`));
localhost Postman test working
After deploy to server and the result not working
It happened because I had an DB configuration error, solved it with instead of using tempCont.release(); I omit it and used only the response, by doing this I have solve the error - TypeError: Cannot read property 'release' of undefined, which caused 503 Service Unavailable. Then after solving DB configuration issue I have again added the tempCont.release(); now it works fine.
router.post('/login', (req, res) => {
connection.getConnection((error, tempCont) => {
if (error) {
// tempCont.release();
res.json({ message: 'Can not connect database' });
}
else { ....

Error occurred while trying to proxy request [...] from 192.168.0.4:3000 to http://192.168.0.4:5000 (ECONNRESET)

I'm working on my first project using react and node and have been stuck on this problem for a while. I keep getting this error when trying to connect to my site using the ip address, but if I just do localhost:3000 it works perfectly. I want to be able to connect via the IP address so I can test it across devices. Here is the full error:
[HPM] Error occurred while trying to proxy request /socket.io/?EIO=3&transport=polling&t=N4EqtUl&sid=kz_I098ZM2h1Z6WZAAAI from 192.168.0.4:3000 to http://192.168.0.4:5000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
I checkout out this post and implemented setupProxy.js like the second answer suggested, but it still isn't working.
Here is my node server (index.js):
const express = require('express');
const app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const path = require('path')
const port = process.env.PORT || 5000;
app.use(express.static(path.join(__dirname, 'client/build')));
io.on('connection', function(socket) {
console.log('a user connected');
});
// Anything that doesn't match the above, send back index.html
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
})
http.listen(port || 5000, function () {
console.log('listening on', port);
});

Initializing ws server with express

I have been struggling to find the source of an error in the following code:
const express = require("express");
const path = require("path");
const WebSocket = require("ws");
const SocketServer = WebSocket.Server;
const PORT = process.env.PORT || 3000; // port to listen on
const INDEX = path.join(__dirname, "index.html"); // index address
var server = express();
server.use((req, res) => res.sendFile(INDEX));
server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
const wss = new SocketServer({server}); // wss = web socket server
(I have code below for connection etc. that is irrelevant to this question I think)
Upon running this, I receive the following error from the client in the browser:
WebSocket connection to 'ws://localhost:3000/' failed: Connection closed before receiving a handshake response
What is confusing me is that the code works if I make the following change:
var server = express()
.use((req, res) => res.sendFile(INDEX));
.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
The problem with this method is that it does not work to add in another .use before the existing .use, which is necessary for accessing static files (i.e. javascript as its own file instead of in an HTML file)
Why does changing my code between these two examples break it? What can I do to resolve this?
Thank you
You need to pass the http.createServer instance to the WebsocketServer. Not express instance
var app = express();
var server = http.createServer(app);
I found a solution!
It turns out that you must set the value of the server variable when .use and .listen are called.
var server = express();
server = server.use((req, res) => res.sendFile(INDEX));
server = server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
instead of
var server = express();
server.use((req, res) => res.sendFile(INDEX));
server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));

error conexion vue-socket ERR_CONNECTION_REFUSED

I have not managed to connect my vue js to my nodejs server. The configuration is the following, server side:
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
app.use(express.static(__dirname + '/dist'));
app.set('port', process.env.PORT || 5050);
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
server.listen(app.get('port'), () => console.log(`http://localhost:${app.get('port')}`));
on the client side:
Vue.use(new VueSocketio({
debug: true,
connection: 'http://localhost:5050'
}));
when I test in local everything works perfect, the socket opens the events are issued without failure, the problem lies when I upload a server, the constant error:
vue-socketio.js:1 GET http://localhost:5050/socket.io/?EIO=3&transport=polling&t=MUu_7m4 net::ERR_CONNECTION_REFUSED
try to put the ip of the server provider and still the problem, I have seen many issues regarding this but I can not get a clear solution, I appreciate the help they can give me, thanks!

Express launching Angular application

I have an express server setup online which loads multiple ports and those ports are setup on subdomains for example. port 9000 loads the main domain.com port 8000 loads the main application at "app.domain.com" port 1000 loads "signup.domain.com" and the build version of the app is on port 8500 "build.domain.com".
The application is an Angular application however when I go to load the Angular app it loads on port 4200 or it says 8500 is in use. So currently I am loading that in express like so:
// Build Application - In Development
var appbuild = express();
appbuild.get('/', function (req, res){
res.sendFile('/app/build/myapp/src/index.html', { root: '.' })
});
var port = 8500;
appbuild.listen(port);
console.log('Build App Listening on port', port);
So my question is in Express how can I instead of writing sendfile command make it launch the angular app in that location on port 8500 so my subdomain names will work. The reason I'm asking this is because right now all it does is load the index file but angular or the app isn't running so i just see source code that says app-root and a blank white page.
Thank you in advance.
Robert
--- Update. I've decided to post the entire Express file. My issue is trying to load a angular app on port 8500 from the subfolder upon booting of express. Here is the full server.js code:
// server.js
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
cors = require('cors'),
mongoose = require('mongoose'),
config = require('../config/DB');
mongoose.Promise = global.Promise;
mongoose.connect(config.DB).then(
() => {console.log('Database is connected') },
err => { console.log('Can not connect to the database'+ err)}
);
// Main Website
var web = express();
web.get('/', function (req, res){
res.sendFile('/web/index.html', { root: '.' })
});
var port = 9000;
web.listen(port);
console.log('Web Listening on port', port);
// Main Application
var app = express();
app.get('/', function (req, res){
res.sendFile('/app/index.html', { root: '.' })
});
var port = 8000;
app.listen(port);
console.log('Main App Listening on port', port);
// Build Application - In Development
var appbuild = express();
appbuild.get('/', function (req, res){
res.sendFile('/app/build/myapp/src/index.html', { root: '.' })
});
var port = 8500;
appbuild.listen(port);
console.log('Build App Listening on port', port);
// Sign up Portal
var sign = express();
sign.get('/', function (req, res){
res.sendFile('/signup/index.html', { root: '.' })
});
var port = 10000;
sign.listen(port);
console.log('Sign Up Portal Listening on port', port);
Refer to this link https://malcoded.com/posts/angular-backend-express
Update your code to the following:
const express = require('express');
const app = express();
app.listen(8500, () => {
console.log('Server started!');
});
You need to build the angular app if your angular version not 1.x
ng build
Also, I think this question is similar to your question:
Not able to view Angular app via express/heroku?

Resources