I believe there may be a gap in the communication between TypeScript, JavaScript, Angular, Nginx, Alpine, and Docker within the network using Nginx. I am

After transitioning to Docker in order to create a virtual network to mimic a real network (using a bridge type with DNS that resolves the FQDN correctly to the corresponding IP), I encountered the following errors in the console.log - no data is being 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 }

This is the browser's (Firefox) console.log

I suspect Nginx may be manipulating the headers, or the body could be empty due to server-side configurations with Nginx.

Everything worked fine on localhost.

I am currently working with the Nginx config, but have not had much success. I have come across similar problems, but the solutions I found did not work with my setup. I tried changing the IP to 0.0.0.0 to make it accessible in the network.

Oh, and I am using Node.js and Express.js.

I am using a Dockerfile and docker-compose.yml to create the images, and a PowerShell script to compose the images.

The potential cause of the issue is:

In the backend:

The index.js is up and running and looks like this

"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();
});
// Middlewares
app.use(express_1.default.json());
app.use(express_1.default.urlencoded({ extended: false })); //changed to see whether it would affect the package issue- should allow
app.use(Routes_1.default);
app.listen(4000,'0.0.0.0'); // or FQDN 'frontend'
console.log('server on port', 4000);

This was generated from index.ts and a build command.

The corresponding 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 the following in the usr/share/nginx/html directory:

 npm i express
 npm i pg
 node index.js

Then I install Vim and edit the nginx.config like this

  vi /etc/nginx/nginx.conf

I add a server directory, have it listen to the FQDN 'frontend' or its corresponding IP and port 4000

 listen ip:port kind of syntax

Earlier, I added error and access logs, and there were no issues except for sometimes it stating that IPs are not available. I lack the understanding to interpret that.

PostgreSQL is also running in a Docker container on the default port 5432, with the FQDN of the database being properly resolvable, along with the backend's FQDN.

There is much more information linking the small pieces of code I have. Feel free to inquire for more details if needed or if you believe it would help pinpoint the issue.

Answer №1

I have learned a valuable lesson. Servers listen to their own IPs or at their localhost, leading me to a misconception. I am grateful to those who took the time to review this.

Additionally, a Node.js Express server does not always require Nginx to operate. Node alone is sufficient for the task. Correcting these misunderstandings resulted in the functionality working as intended :)

This issue can now be closed or serve as a reminder for two important points:

1. Understanding the conceptual idea of how networks function 2. Understanding the technology stack being utilized and how it operates

Otherwise, mark as /closed

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with click event not triggering initial interaction with dynamically inserted elements through AJAX requests

I am experiencing an issue with my ul element that is being populated with li elements through AJAX. When I try to click on these li elements, something peculiar happens. The functionality only works when I click for the second time, not the first. Despit ...

Ensuring the model accurately reflects the input's value attribute in AngularJS

I am in the process of creating a set of image "radio buttons" where only one can be selected per group. However, as a newcomer to Angular, I'm facing difficulties in maintaining the value and using it as my ng-model. Additionally, I am looking for a ...

Compiling this HTML template in dev mode with Vue is agonizingly slow

I've been working with a template app setup that was bootstrapped using vue CLI. Within one of my components, I have 20 nested div tags. During development mode, it's taking roughly 10 seconds to compile this component. The more deeply I nest HTM ...

Navigate to items within a content block with a set height

I have a <div> that has a fixed height and overflow-y: scroll. Inside this div, there is mostly a <p> tag containing a long text with some highlighting (spans with background-color and a numbered id attribute). Just to note, this is an HTML5 a ...

The special function fails to execute within an "if" condition

As a newcomer to JavaScript/jQuery and Stack Overflow, I kindly ask for your patience in case there are any major errors in my approach. I am currently developing an HTML page with Bootstrap 3.3.7, featuring a pagination button group that toggles the visib ...

In React, when pushing to an array, it is the index that gets returned instead of the items

I'm currently working on incorporating a new object into an array that I'm fetching dynamically through my API. Users fill out a form, and the data is transmitted from the form to the state. The initial fetch stores the original array in a React ...

Unable to show information within a view in an Express Node.js application

Just diving into the world of express and node, I'm currently working on a basic express app that retrieves data from a json file. However, when I try to render the data on my post/details view, it doesn't seem to show up. I suspect the issue lie ...

Angular2 encountering a lack of service provider issue

After finding the code snippet from a question on Stack Overflow titled Angular2 access global service without including it in every constructor, I have made some modifications to it: @Injectable() export class ApiService { constructor(public http: Http ...

JSON object containing elements with dash (-) character in their names

While I am in the process of parsing a `json` object, I encountered an element labeled as `data-config`. Here's an example: var video = data.element.data-config; Every time I attempt to parse this specific element, an error pops up: ReferenceError ...

Disregard all numbers following the period in regex

I have developed a function to format numbers by adding commas every 3 digits: formatNumber: (num) => { return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,') }, The issue with this function is that it also add ...

The JavaScript code in the HTML files is not functioning as expected

Upon form submission in my program, the form action directs to xyz.php. Within xyz.php, I simply use INCLUDE("abc.html") to redirect to another HTML page. The abc.html page contains HTML tags and JavaScript code, but after the form submission, the page red ...

Copy the click function to a contenteditable <div> with spellcheck feature

When using a contenteditable <div> in Chrome, the native spell check feature will only work if the user manually clicks into the <div>. But what if you want to add a contenteditable <div> dynamically? Is there a way to trigger the spell c ...

Issue with Date Picker not properly storing selected date in MySQL database

My goal is to save the datepicker date to MySQL by selecting the date format using JavaScript. I have verified that the date format appears correct as YYYY-MM-DD when logging to the console. However, when I try to execute an INSERT query to MySQL, the date ...

Having trouble with your UI Router states not updating correctly when changing the URL in your Angular UI Router?

I have tried numerous solutions and tutorials already with no success in finding the right answer. Therefore, I am posting here in hopes of receiving a fresh perspective and a little challenge. Using: angular ui router The Issue I have set up differen ...

Leveraging jQuery for validating a value by comparing it to another input value

Currently, I am working on a validation task for input fields related to hour rates. The aim is to ensure that once a base hour input is added, the other input values cannot exceed the value entered in the first base rate column. Essentially, the initial i ...

Transferring a variable between a JavaScript function and a Java class

Currently, I am working with STS and building an application that includes HTML files and JavaScript. Within this file, there is a function that configures variables. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www ...

Using AJAX to assign PHP session variables

Is there a way to update the SESSION variable named "fullname" on Page 2 without causing the page to refresh? This is my attempt using AJAX: HTML code for Page 1: <input type="text" name="fullname" id="fullname" placeholder="Full name"> <butto ...

Receiving an error with React Proptypes when using the union type Breakpoint

Struggling to assign the correct proptype to the material-ui Breakpoint type. The breakpoint values are: export type Breakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl'; In my App.tsx file, I have the following ...

The type 'number | { percent: number; }' cannot be assigned to the type 'string | number | undefined' according to ts(2322) error message

Presently, I am engaged in a project utilizing React and Typescript, where I am working on implementing a progress bar using react semantic-ui. I have encountered a typescript error in my code. Here is the segment causing the issue: import React, { Compo ...

Transfer the parameter from ajax function to the aspx.cs file

I've written some code using HTML, JS, and AJAX, but it's not functioning as expected. <script type="text/javascript"> function DeleteSelectedRecord(id) { alert(id); $.ajax({ type: "POST", ...