当找不到路径时,Undertow ResourceHandler返回相同的文件

时间:2018-09-30 07:12:23

标签: reactjs kotlin undertow

我正在使用undertow静态地为React单页应用程序提供服务。为了使客户端路由正常工作,我需要为服务器上不存在的路由返回相同的索引文件。 (有关问题click here的更好解释。)

当前使用以下ResourceHandler实现:

ResourceHandler(resourceManager, { exchange ->
    val handler = FileErrorPageHandler({ _: HttpServerExchange -> }, Paths.get(config.publicResourcePath + "/index.html"), arrayOf(OK))
    handler.handleRequest(exchange)
}).setDirectoryListingEnabled(false)

它可以工作,但是很老套。我觉得必须有一种更优雅的方式来实现这一目标?

1 个答案:

答案 0 :(得分:0)

我在underwow文档中找不到我需要的东西,因此不得不使用它来寻求解决方案。该解决方案适用于嵌入式Web服务器,因为这正是我所追求的。我正在尝试为带有路由的Angular 2+单页应用程序执行此操作。这就是我到达的:

%module libe57format

%include "stdint.i"
%include "std_string.i"
%include "std_vector.i"

namespace std {
    typedef signed char     int8_t;
    typedef short int       int16_t;
    typedef int         int32_t;
    typedef long int        int64_t;
    typedef unsigned char       uint8_t;
    typedef unsigned short int  uint16_t;
    typedef unsigned int        uint32_t;
    typedef unsigned long int   uint64_t;
}

//Things that do not work:
%nodefaultctor e57::Node;
%ignore e57::Node::Node();
%ignore e57::Node::destImageFile() const;
//%ignore e57::Node::parent() const;
%ignore e57::Node::operator ==(e57::Node) const;
%ignore e57::Node::__equals__(e57::Node) const;
%ignore e57::Node::operator !=(e57::Node) const;
%ignore e57::Node::__notEquals__(e57::Node) const;
%ignore e57::StructureNode::StructureNode(ImageFile);
%ignore e57::StructureNode::destImageFile() const;
%ignore e57::StructureNode::parent() const;
//%ignore e57::StructureNode::get(const ustring&) const;
//%ignore e57::StructureNode::get(int64_t) const;
%ignore e57::StructureNode::operator Node() const;
%ignore e57::VectorNode::VectorNode(ImageFile);
%ignore e57::VectorNode::VectorNode(ImageFile, bool);
%ignore e57::VectorNode::destImageFile() const;
%ignore e57::VectorNode::parent() const;
%ignore e57::VectorNode::append(Node);
%ignore e57::VectorNode::get(const ustring&) const;
//%ignore e57::VectorNode::get(int64_t) const;
%ignore e57::VectorNode::operator Node() const;

//%ignore e57::SourceDestBuffer::SourceDestBuffer;

%{
#include </usr/local/include/E57Format/E57Format.h>
%}

%template(SDBVector) std::vector<e57::SourceDestBuffer>;


%ignore e57::BlobNode;
%ignore e57::ScaledIntegerNode;
%ignore e57::CompressedVectorWriter;

%rename(__type__) e57::Node::type;
%rename(__equals__) e57::Node::operator==;
%rename(__notEquals__) e57::Node::operator!=;
%rename(__equals__) e57::ImageFile::operator==;
%rename(__notEquals__) e57::ImageFile::operator!=;
%rename(__Node__) e57::StructureNode::operator Node;
%rename(__Node__) e57::VectorNode::operator Node;
%rename(__Node__) e57::CompressedVectorNode::operator Node;
%rename(__Node__) e57::IntegerNode::operator Node;
%rename(__Node__) e57::ScaledIntegerNode::operator Node;
%rename(__Node__) e57::FloatNode::operator Node;
%rename(__Node__) e57::StringNode::operator Node;
%rename(__Node__) e57::BlobNode::operator Node;

%include </usr/local/include/E57Format/E57Format.h>

这是它的作用:

  • “ myCustomServiceHandler”为服务器端逻辑提供处理程序,以处理发送到服务器的查询
  • 'ResourceManager / FileResourceManager'提供位于应用程序的(Angular)根路径中的文件
  • 如果查询是针对客户端路由路径而非实际文件的,则“ FileErrorPageHandler”将提供应用程序的“ index.html”页面。如果文件请求错误,它也会提供该文件。

请注意第一个'MY_PREFIX_PATH'之后的下划线'_'。我想让应用程序API URL与Web路径相同,但是没有多余的逻辑,我选择了下划线。