Web Front-end
JS Tutorial
Guide to locating and resolving 404 errors in front-end and back-end integration between Angular and Node.js
Guide to locating and resolving 404 errors in front-end and back-end integration between Angular and Node.js

This article explains in detail the root cause of HttpErrorResponse: 404 Not Found when the Angular front-end calls the Node.js back-end API. It focuses on key issues such as inconsistent routing configurations, mismatched basic paths, and hidden CORS failures, and provides immediately verifiable repair solutions.
This article explains in detail the root cause of `HttpErrorResponse: 404 Not Found` when the Angular front-end and back-end call the Node.js back-end API. It focuses on key issues such as inconsistent routing configurations, basic path mismatches and CORS hidden failures, and provides immediately verifiable repair solutions.
In Angular Node.js full-stack development, the front-end console reports HttpErrorResponse: 404 Not Found (such as POST http://localhost:8080/add 404), but the Postman call is successful - this is not a problem of the network or service not being started, but a typical symptom that the request path is not correctly matched by the backend . The core contradiction is that the request URL initiated by Angular is inconsistent with the routing path actually registered by Express .
? Key problem location: routing prefix misplacement
Observe your server code:
// server.js
app.use('/add', patientrouter); // ← Mount middleware to the '/add' base path
What is defined in patient.route.js is:
patientrouter.post("/", Patientcreate); // ← Actual processing of POST /add/
This means the full accessible path is POST http://localhost:8080/add/ (note the trailing slash).
But your Angular PatientService is configured as:
const baseUrl = 'http://localhost:8080/add'; // ← Missing trailing slash // Call this.http.post(baseUrl, data) → actually issue POST /add (without trailing slash)
Express's app.use('/add', router) will not automatically match /add (no trailing slash) by default - it only matches paths starting with /add/ (such as /add/, /add/foo). Therefore /add itself is treated as an independent route by Express, is undefined, and returns 404.
✅Repair plan one (recommended): Unify path specifications
- ✅ Backend: Ensure that the mount path is semantically consistent with the sub-route
// server.js — modified to explicitly support /add and /add/ app.use('/add', patientrouter); app.post('/add', (req, res) => { patientrouter(req, res); // or redirect directly}); - ✅ Frontend: Explicitly add trailing slash in baseUrl
const baseUrl = 'http://localhost:8080/add/'; // ← Key: add '/' at the end
? CORS is not the main reason, but it needs to be verified whether it really takes effect.
You have cors({ origin: "http://localhost:8080" }) enabled, but note:
- Angular runs at http://localhost:4200 (not 8080) by default, and your corurl.origin writes http://localhost:8080 → Origin does not match, CORS will be intercepted by the browser , and the error should be CORS policy: No 'Access-Control-Allow-Origin' header, not 404.
- ✅ The correct configuration should be:
const corurl = { origin: "http://localhost:4200" }; // Angular default port // or more securely allow all development sources (development environments only): // const corurl = { origin: true };
? Tip: Open the browser developer tools → Network tab → click the failed request → View Headers → Request URL and Response → Status . If the status code is 404, it is 100% a routing problem; if the preflight request (OPTIONS) fails and a CORS error is reported, the origin needs to be corrected.
✅ Complete runnable repair steps
-
Update the CORS configuration of server.js :
import cors from "cors"; // ... const corurl = { origin: "http://localhost:4200", credentials: true }; app.use(cors(corurl)); -
Correct baseUrl of patient.service.ts :
const baseUrl = 'http://localhost:8080/add/'; // ← must have trailing slash
-
Enhance backend fault tolerance (optional but recommended) : Add a 404 log at the top of server.js to facilitate debugging:
app.use('*', (req, res) => { console.log(`⚠️ Unmatched route: ${req.method} ${req.originalUrl}`); res.status(404).json({ error: 'Route not found' }); }); -
Restart the service and verify :
- Start the Node.js service (node server.js or npm start)
- Start Angular (ng serve)
- Visit http://localhost:4200/add in the browser and submit the form
- Check the browser Network panel: the request URL should be http://localhost:8080/add/, and the status code should be 200 or 201
⚠️ Notes and best practices
- Port consistency : Confirm that the Node.js service listening port (currently 8080) is exactly the same as the front-end request address; if process.env.PORT has been modified, please check it simultaneously.
- Production environment proxy : You can use Angular CLI's proxy.conf.json during the development phase to avoid cross-domain and hard-coded URLs:
{ "/add": { "target": "http://localhost:8080", "secure": false, "changeOrigin": true, "pathRewrite": { "^/add": "/add" } } }Then baseUrl = '/add/', forwarded by the Angular proxy.
- API versioning suggestion : In long-term projects, design the API path to /api/v1/patients instead of /add, which has clearer semantics and is easier to maintain.
Through the above adjustments, HttpErrorResponse: 404 will completely disappear, and the front-end and back-end data flows can be stably connected. Remember: 404 means that the server says "I did not find this address", not that the front end cannot send it out - first verify the URL, and then check the network and permissions.
The above is the detailed content of Guide to locating and resolving 404 errors in front-end and back-end integration between Angular and Node.js. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20566
7
13666
4
useEffect in MERN stack React application implements instant update of user information after login
Apr 04, 2026 am 06:57 AM
This tutorial deeply explores the problem of useEffect hook in the MERN stack React application. After the user logs in, the user information cannot be updated immediately, and the page needs to be refreshed to display the latest data. The article analyzes in detail the correct use of the useEffect dependency array, points out common errors, and provides a dependency management solution based on user status changes to ensure that user information can be responded to and updated immediately after logging in, thereby improving the user experience.
Dynamic data transfer and management within scripts in Node-RED UI templates
Apr 04, 2026 am 06:54 AM
This article aims to solve the problem in Node-RED UI templates that cannot directly use Mustache syntax to dynamically inject JavaScript code within tags. We will explain the root cause and provide two safe and effective solutions: one is to directly access the passed data through $scope.msg, and the other is to render the data into JavaScript variables through Mustache combined with the | json filter. In addition, the article will also discuss the practice of encapsulating UI templates into subflows (Subflow) to achieve template reuse and centralized management.
The correct implementation method in Svelte to avoid #each loop causing repeated display of related articles
Apr 03, 2026 am 08:48 AM
This article introduces how to correctly filter and display "related articles" in Svelte without duplicates, and solve the problem of the same article being rendered multiple times due to nested #each. The core is to forward the tag matching logic to the JavaScript layer to complete the filtering, instead of double traversing in the template.
JavaScript form validation: use custom CSS classes for precise control
Apr 04, 2026 am 05:42 AM
This tutorial dives into how to achieve precisely controlled form validation using pure JavaScript and custom CSS classes, rather than relying on browser default pseudo-classes. This article will analyze in detail the difference in verification timing between input and change events, and provide an optimization solution that effectively solves the problems of lagging verification status update and inaccurate class name assignment by combining the change event and the checkValidity() method, thereby achieving more flexible and user-friendly real-time form feedback.
A complete solution for retaining the ability to scroll vertically while scrolling horizontally
Apr 05, 2026 am 08:31 AM
This article provides a robust horizontal scrolling JavaScript implementation that intelligently determines the scrolling direction and boundary status to ensure that users can not only slide to the right to browse the content, but also scroll up to return to the initial area at the top.
A deep understanding of JavaScript array loops and their potential risks
Apr 04, 2026 am 06:42 AM
This article aims to delve into the concept of looping arrays in JavaScript, its potential risks, and how to effectively avoid these problems. We'll clarify some common misunderstandings about looping arrays, show through code examples what situations can lead to infinite loops or stack overflows, and provide safe alternatives to help developers better understand and work with this type of data structure.
How to add focus style to parent container only on keyboard navigation (avoid triggering on mouse click)
Apr 03, 2026 am 07:57 AM
This article details how to accurately implement "add visual focus style to the parent container only when the child input box gains focus through the keyboard" without relying on the experimental CSS :has(). It covers native TypeScript solutions and recommended React ARIA practices, taking into account accessibility, testability and browser compatibility.
A complete solution on how to implement product variations (such as sizes) and inventory limits in Stripe
Apr 05, 2026 am 08:36 AM
Stripe's custom_fields are not suitable for product variant selection and inventory management; the correct approach is to create an independent Product/Price for each variant, and implement inventory verification, preemption and synchronization logic at the application layer.





