Home Web Front-end JS Tutorial Detailed interpretation of Angular's error 404 issues

Detailed interpretation of Angular's error 404 issues

Jun 08, 2018 pm 04:30 PM
404 angular found NOT

This article mainly introduces the error 404 (not found) that explains Angular debugging skills in detail. Now I share it with you and give you a reference.

Preface

During the holidays, students are busy recharging their batteries, and there are many who learn "Full Stack Development". I often receive feedback from readers. When debugging "Full Stack Development" I encountered confusion when using examples from the book "The Way of Development". Although you will encounter various problems, generally speaking, they can be boiled down to one technical point, and that is - Angular debugging.

Writing Angular code is not difficult, but debugging Angular code is difficult. For those new to Angular, debugging Angular requires a process and continuous accumulation of experience in this process. As soon as you see an error report, you can guess where the problem lies.

In this article, let’s start with common Angular debugging skills. What should we do if we encounter a 404 (not found) error?

Simulate a debugging scenario

Example from the book "Full Stack Development" Chapter 6.3.3 - Implementation of a single-page application, to implement such a The effect is as shown in the figure:

Single page application effect

When you click Home, About, and Contact, the content below will change accordingly. Without the need for the page to send a new request. A single-page application automatically changes within a page without having to jump back and forth to a new page. The user experience is greatly improved.

In order to implement such a single-page application, the book creates html files (index.html, home.html, about.html, contact.html), and a myapp.js for router and controller. And created the application entrance server.js

Follow the tutorial in the book and complete the code input step by step. Until finally running node server.js, I found that when I clicked the Home, About, and Contact buttons, the The content has not changed. There is no problem with the code, so what's going on?

Debugging tips

Friendly reminder:

When debugging Angular, be sure to use the Chrome browser. This is easy to understand. Angular is owned by Google, and Chrome is also owned by Google. There is no doubt that Chrome supports its own products best.

Run node server.js in the terminal window

In the Chrome browser, enter: http://localhost:3000

At this time, open the development of the Chrome browser Tools, if it is a Wiindows system, use the F12 key; if it is a Mac system, use Option Command I. The screenshots here take the Mac system as an example.

Angular error message

Let’s first look at the error in the first line. Many times, the error in the second line is caused by the first line; if the first The error is resolved and the error below will disappear automatically.

http://localhost:3000/myapp.js 404 (Not Found)

In network terms, 404 means that the file you want to request does not exist. In traditional development environments, such as C and Java, even if the file cannot be found, a 404 error will not occur. Look at the next prompt Not Found, which means it was not found.

Error troubleshooting method

When encountering this kind of error, the troubleshooting method is to first check whether the file exists, and then check whether its access path is correct.

(1) Confirm whether the file exists. The file names must be strictly consistent, and the file names themselves are not case-sensitive. For example: index.html and Index.html are the same file, and myapp.js and myApp.js are also the same thing.

(2) Check the path of the file. Checking whether the file name is correct is simple; checking the file path is a technical task. Why do you say that? This is because, in AngularJS, there is a concept of static resources. The myApp.js file clearly exists, why can’t it be found? Let's first look at the line of code that references myApp.js.

In the tag of index.html:

<script src="myapp.js"></script>
Copy after login
Copy after login

When referencing a file, either an absolute path or a relative path is given. What is the file path of myapp.js? What about the agreement?

This depends on how the server.js file specifies the path convention. The code is as follows:

app.use(express.static(path.join(__dirname, &#39;public&#39;)));
Copy after login

__dirname is a path, which refers to the path where the current file (server.js) is located, and public is the lower-level directory of __dirname. path.join is a specified form, and express.static represents the location where the static resources specified by the application are stored.

According to this concept, the four files above home.html, about.html, contact.html, and myapp.js are all static resource files. Let’s look at this line of code again:

<script src="myapp.js"></script>
Copy after login
Copy after login

Code When written this way, the application will traverse the myapp.js file in the public directory of the current path. After reading this, you will understand the reason for the error 404.
You need to put these static resource files in the public folder, as shown in the figure.

静态资源文件路径

进一步讨论

既然是路径惹的祸,那么能不能换一种思路,改一下静态资源路径的设置呢? 比如,修改server.js 文件的代码,如下:

app.use(express.static(path.join(__dirname, &#39;/&#39;)));
Copy after login

这样一来,上面那四个文件就不用放到public了, 这种方法,虽然没有报错,问题是,这不是规范的做法。但凡稍微复杂一点的应用,都会用到大量的静态资源文件,如果不分配路径,将难以维护!

小结

掌握一门编程技术, 仅仅会编写代码是不够的。 评价一个人的编程水平,就看他的调试技能是否驾轻就熟。 后续,我要借助一些 Angular专用的调试工具,模拟一些出现bug的场景,看看如果快速发现root cause,并给出解决方案。

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

使用Vue.set()如何实现数据动态响应

在vue中如何实现动态绑定图片以及data返回图片路径

在vue中如何实现动态改变静态图片以及请求网络图片

The above is the detailed content of Detailed interpretation of Angular's error 404 issues. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Let's talk about metadata and decorators in Angular Let's talk about metadata and decorators in Angular Feb 28, 2022 am 11:10 AM

This article continues the learning of Angular, takes you to understand the metadata and decorators in Angular, and briefly understands their usage. I hope it will be helpful to everyone!

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

Angular + NG-ZORRO quickly develop a backend system Angular + NG-ZORRO quickly develop a backend system Apr 21, 2022 am 10:45 AM

This article will share with you an Angular practical experience and learn how to quickly develop a backend system using angualr combined with ng-zorro. I hope it will be helpful to everyone!

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

Is the 404 code a front-end or back-end problem? Is the 404 code a front-end or back-end problem? Jun 12, 2023 pm 01:53 PM

404 codes are situation-specific and can be either front-end or back-end issues. In some cases, 404 errors can come from problems on the client side, such as spelling in the URL path that results in the page not being found. In other cases, , 404 errors may be caused by the web application not responding to the request correctly, such as missing interface implementation or empty database query results.

See all articles