标签: http-status-code-404

Express.js 路由器发布请求返回 404

当我尝试从 html 表单发送请求时,我的 Express 服务器返回 404 错误。但是 get 请求工作正常。我已将服务器和路由的文件分开。我无法在 app.js 中发出请求,因为请求太多(针对不同的页面)。我想向数据库发出插入后请求,但我无法从表单中获取任何信息。

我有什么遗漏的吗?

我的 html 表单(确切地说是 ejs)

<form action="/add" method="POST">
     <input type="text" name="title" placeholder="Event name" />
     <input type="text" name="subcategory" placeholder="Category" />
     <input type="text" name="date" placeholder="Date" />
     <input type="text" name="place" placeholder="Place" />
     <input type="text" name="organisator" placeholder="Organisator" />
     <textarea name="description" placeholder="Description" onkeyup="adjust_textarea(this)"></textarea>
     <input type="submit" value="Add" />
 </form>
Run Code Online (Sandbox Code Playgroud)

我的 App.js 文件(404 处理程序错误)

const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

var app …
Run Code Online (Sandbox Code Playgroud)

javascript post node.js http-status-code-404 express

2
推荐指数
1
解决办法
7999
查看次数

Laravel 修改路由参数为 name 列而不是 id

在我的web.php文件中,我指定了以下路线:

Route::get('/{project}', 'ProjectsController@index');
Run Code Online (Sandbox Code Playgroud)

在我的 ProjectsController 中,我定义公共函数index如下:

use App\Project;
// ... Here is the class declaration etc.
public function index(Project $project) {
  dd($project->name);
}
Run Code Online (Sandbox Code Playgroud)

目前,我的项目表中有一个条目,我可以通过我的雄辩模型毫无问题地调用它。这是我的条目:

Name: sampleproject
Description: This is a test.
ID: 1
// And the timestamps...
Run Code Online (Sandbox Code Playgroud)

当调用 时/sampleproject,它会返回 404 错误页面。
[...]

更新:当调用/1,即项目 id 时,一切都按预期进行。如何修改我的代码,以便我可以通过项目名称而不是 id 调用我的控制器?

php variables http-status-code-404 laravel laravel-5

2
推荐指数
1
解决办法
1123
查看次数

定制 404 Laravel

如果我们只是404.blade.php在其中创建一个页面resources/views/error就可以正常工作,但是Auth()在 404 页面上不起作用,要解决这个问题,如果我们遵循 stackoverflow 上提供的解决方案,Laravel 身份验证错误将停止工作。我使用以下解决方案来完成这项工作。

php http-status-code-404 laravel laravel-authentication

2
推荐指数
1
解决办法
4994
查看次数

Symfony 5 自定义 404 页面

我正在尝试为 Symfony 5 项目创建一个自定义 404 页面,该页面必须:

  1. 输出一个简单的 JSON 编码字符串,例如“未找到”。
    • 所述字符串必须从翻译资源中读取。
  2. 有一个额外的Content-Type: application/json标题。

Symfony 文档中有一个部分试图解释如何实现这一点,但该信息似乎不完整/不正确,显然是为 4.X 版本编写的,甚至指向 GitHub 上不存在的源文件。

我已经成功创建了一个错误控制器,但它吞噬了所有错误:

# config/packages/framework.yaml
framework:
    error_controller: App\Controller\ErrorController::errorHandler
Run Code Online (Sandbox Code Playgroud)
// src/Controller/ErrorController.php
class ErrorController extends AbstractController
{
    public function errorHandler(TranslatorInterface $translator) : JsonResponse
    {
        return new JsonResponse($translator->trans('not_found'));
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是这会导致任何错误(包括内部错误)返回 404 页面。

如何让这个控制器/方法只处理 404 错误,而让框架本身像以前一样处理其他所有内容?

http-status-code-404 symfony symfony5

2
推荐指数
1
解决办法
3216
查看次数

模拟新的 HttpClientErrorException.NotFound

我有一个返回的第三方服务HttpClientErrorException.NotFound。当它返回此异常时,我从应用程序中抛出异常,表示输入无效,但对于所有其他异常(例如服务不可用等),我需要使用默认值并进一步继续。

我的代码块如下:

public String callService(String input)
    {
        String value = "";
        try
        {
            value = service.callMethod(input);
        }
        catch(HttpClientErrorException.NotFound e1)
        {
            throw new ApplicationException("Invalid Input")
        }
        catch(Exception e)
        {
            value = "US";
        }
        return value;
    }
Run Code Online (Sandbox Code Playgroud)

当我为此编写 JUnit 时,如何模拟 service.callMethod(input) 的调用并返回HttpClientErrorException.NotFound

我尝试模拟并发送如下状态代码,但它不起作用。

Junit测试用例方法如下:

@Test(expected = ApplicationException.class)
public void callServiceInvalidInput() throws ApplicationException 
{
    String inputValue = "JLR";
    when(externalService.callMethod(inputValue))        
    .thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));
    
    String result = handler.callService(inputValue);
}
Run Code Online (Sandbox Code Playgroud)

当我模拟服务调用时,执行的 catch 子句是 Exception e 而不是前一个。原因是对象e在模拟时是instanceof HttpClientErrorException;但是当实际的服务调用发生时,它是 HttpClientErrorException$NotFound 的实例

java rest junit mockito http-status-code-404

2
推荐指数
1
解决办法
5906
查看次数

EntityNotFoundException 情况下的 HTTP 状态

我正在研究我的应用程序中的异常处理。在我的服务中我有这个方法:

public Optional<Entity> getEntityById(Long id) {
    return Optional.of(EntityMapper.fromEntityToDto(repository
            .findById(id)
            .orElseThrow(() -> new EntityNotFoundException("No entry was found for" + " id: " + id))));
}
Run Code Online (Sandbox Code Playgroud)

在我的异常处理程序中,我有以下内容

@ControllerAdvice
public class ControllerAdvisor extends ResponseEntityExceptionHandler {

    @ExceptionHandler(EntityNotFoundException.class)
    public ResponseEntity<Object> handleEntityNotFoundException(EntityNotFoundException ex, WebRequest request) {

        ErrorResponse errorResponse = new ErrorResponse(HttpStatus.NOT_FOUND, "Entity not found", ex.getMessage());
        return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里这里看到过他们这样做的例子,但在我看来,这就像滥用HttpStatus. 该资源是 API 端点(这很好),而不是未找到的实体。处理这个用例的最佳方法是什么?


根据 IQbrod 的回答,我创建了一个例外:

@ResponseStatus(value = HttpStatus.NO_CONTENT)
public class MyEntityNotFoundException extends RuntimeException {

    public MyEntityNotFoundException(String message) { …
Run Code Online (Sandbox Code Playgroud)

java exception http-status-codes http-status-code-404

2
推荐指数
1
解决办法
2440
查看次数

如何在为用户提供用户友好内容的同时为谷歌返回适当的404?

我在这里和超级用户之间发帖.如果您觉得这不属于此,请原谅.

我正在观察此处描述的行为- Googlebot正在我的网站上请求随机网址,例如aecgeqfx.htmlsutwjemebk.html.我确信我没有从我网站上的任何地方链接这些网址.

我怀疑这可能是谷歌探讨我们如何处理不存在的内容 - 引用链接问题的答案:

 [google is requesting random urls to] see if your site correctly 
 handles non-existent files (by returning a 404 response header)
Run Code Online (Sandbox Code Playgroud)

我们有一个针对不存在的内容的自定义页面 - 一个风格的页面,上面写着"内容未找到,如果您认为自己错误,请与我们联系",并提供一些内部链接,自然地提供了一个200 OK.URL直接提供(不重定向到单个URL).

我担心这可能会歧视谷歌的网站 - 他们可能不会将用户友好页面解释为,404 - not found并且可能认为我们正试图伪造某些内容并提供重复内容.

我应该如何确保谷歌不会认为该网站是虚假的,同时向用户提供用户友好的消息,以防他们偶然点击死链接?

seo webserver googlebot http-status-code-404

1
推荐指数
1
解决办法
6429
查看次数

当MVC2视图不存在时返回HTTP 404

我只需要一个类似CMS的小型控制器.最简单的方法是这样的:

public class HomeController : Controller {
    public ActionResult View(string name) {
        if (!ViewExists(name))
            return new HttpNotFoundResult();
        return View(name);
    }

    private bool ViewExists(string name) {
        // How to check if the view exists without checking the file itself?
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是如果没有可用的视图,如何返回HTTP 404?

可能我可以在适当的位置检查文件并缓存结果,但这感觉非常脏.

谢谢,
德米特里.

.net asp.net-mvc view http-status-code-404 asp.net-mvc-2

1
推荐指数
1
解决办法
1815
查看次数

Codeigniter 404错误 - 如何判断它们来自何处?

例如,在我的日志中,我有许多重复的行,例如:

ERROR - 2011-07-06 09:19:01 --> 404 Page Not Found --> favicon.ico
Run Code Online (Sandbox Code Playgroud)

有没有办法让我找出谁在调用这些错误的URL?favicon只是一个例子,有一些URL例如显示出攻击的意图,而其他URL则是一遍又一遍地重复相同的错误.基本上我很想知道哪些IP可能会阻塞,以及如果他们有错误的链接会联系哪些网站(或者我应该只使用.htaccess将它们重定向到我的服务器上).

codeigniter http-status-code-404

1
推荐指数
1
解决办法
2535
查看次数

找不到Akka Http返回404

我正在努力实现非常简单的事情。

说,我有一个REST API。当我打电话

/api/recipe/1
Run Code Online (Sandbox Code Playgroud)

我想将一种资源作为json返回。

当我打

/api/recipe/2
Run Code Online (Sandbox Code Playgroud)

404 Not Found HTTP响应应返回。就那么简单。

显然,我缺少关于路由指令如何工作的信息,因为我无法将它们组成以遵守上述逻辑。

不幸的是,我找不到任何具体示例,官方文档也没有特别帮助。

我正在尝试类似的操作,但是代码给出了编译错误:

class RecipeResource(recipeService: RecipeService)(implicit executionContext: ExecutionContext) extends DefaultJsonProtocol {

  implicit val recipeFormat = jsonFormat1(Recipe.apply)

  val routes = pathPrefix("recipe") {
    (get & path(LongNumber)) { id =>
      complete {
        recipeService.getRecipeById(id).map {
          case Some(recipe) => ToResponseMarshallable(recipe)
          // type mismatch here, akka.http.scaladsl.marshalling.ToResponseMarshallable 
          // is required
          case None => HttpResponse(StatusCodes.NotFound)
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

更新资料

以下是recipeService更清晰的代码:

class RecipeService(implicit executionContext: ExecutionContext) {

  def getRecipeById(id: Long): Future[Option[Recipe]] = {
    id match …
Run Code Online (Sandbox Code Playgroud)

scala http akka http-status-code-404 akka-http

1
推荐指数
1
解决办法
3909
查看次数