FRIENDS!
Need help. Can’t figure out what is causing the net :: ERR_CONNECTION_REFUSED error.
Backend and frontend apps work as expected, except for requests / CORS.
I deployed the code to the host according to this article . Naturally, I updated all the packages before going to production.
Host (VPS : CentOS7 , VestaCP ) has nginx + php-fpm , as well as MongoDB , node.js , @ vue / cli . All required ports are open:
Server and client parts are in the same directory / on the same domain. Here is the structure:
All dependencies for the packages are taken into account. The compilation of production and server startup is stable and error free:
What is most interesting, requests in browsers are cut, and through Postman I get / send / update data perfectly:
+++ server.js code +++
let express = require ('express');
let app = express ();
let bodyParser = require ('body-parser');
// Parse requests
app.use (bodyParser.urlencoded ({
extended: true
}));
app.use (bodyParser.json ());
// Load CORS
const cors = require ('cors');
const corsOptions = {
credentials: true,
origin: 'http: // localhost: 4200', // changed to http: // & lt; my domain name & gt;
allowedHeaders: ['Content-Type'],
optionsSuccessStatus: 200
};
app.use (cors (corsOptions));
// Configuring the database
const dbConfig = require ('./ app / config / mongodb.config.js');
const mongoose = require ('mongoose');
mongoose.Promise = global.Promise;
// Connecting to the database
mongoose.connect (dbConfig.url, {
useNewUrlParser: true,
useFindAndModify: false
})
.then (() = & gt; {
console.log ("Successfully connected to MongoDB.");
}). catch (err = & gt; {
console.log ('Could not connect to MongoDB.');
process.exit ();
});
require ('./ app / routes / customer.routes.js') (app);
// Create a Server
let server = app.listen (dbConfig.serverport, () = & gt; {
let host = server.address (). address;
let port = server.address (). port;
console.log ("App listening at http: //% s:% s", host, port)
});
Answer 1, authority 100%
Error found. Everything was solved:
- by replacing the origin option on the server (CORS) : from localhost: 4200 to
http: // & lt; my domain & gt; . - port reconfiguration: listening port 8080 was open but busy by another process. Set up a different listening port on the server and replicate it on the client.
Everything worked. Thank you all! ..