CppCMS-嵌入式 404
CppCMS-Embedded 404
我正在尝试使用 CppCms 的一个简单示例,我想执行与 Web 应用程序根相关的所有应用程序。我不想执行相对于脚本路径的应用程序。 Ex localhost: 8080/script-path/relative-path-to-my-application,而不是这个,我希望我的应用程序的路径是这样的:localhost: 8080/relative-path-to-my-application。我想使用 CppCMS-Embedded 来执行这个应用程序。我正在尝试一个超级简单的例子,但没有成功。我一直尝试根 url (http://localhost:8080/) 我得到这样的 404 错误:
Connection close
Content-Encoding gzip
Content-Type text/html; charset=utf-8
Server CppCMS-Embedded/1.1.0
X-Powered-By CppCMS/1.1.0
status 404 Not Found
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control max-age=0
Connection keep-alive
Cookie _ga=GA1.1.541474771.1454701631
Host localhost:8080
User-Agent Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0
下面是我制作的代码和配置文件:
main.cpp:
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <iostream>
#include "content.h"
class my_app : public cppcms::application{
public:
my_app(cppcms::service& s) : cppcms::application(s){
dispatcher().assign("",&my_app::well_come,this);
mapper().assign("");
mapper().root("");
}
void well_come(){
content::index ci;
ci.message = "Hello ";
render("index",ci);
}
};
int main(int argc,char ** argv){
try{
cppcms::service srv(argc,argv);
srv.applications_pool().mount(
cppcms::applications_factory<my_app>()
);
srv.run();
}catch(std::exception const & e){
std::cerr<<e.what()<<std::endl;
}
}
config.js:
{
"http" : {
"script" : "/mb.fcgi",
"rewrite" : [
{ "regex" : "/media(/.*)?", "pattern" : "[=12=]" },
{ "regex" : ".*" , "pattern" : "/mb.fcgi[=12=]" }
]
},
"service": {
"api":"http",
"port":8080
},
"views" : {
"paths" : [ "./" ],
"skins" : [ "my_app"],
},
}
index.tmpl
<% c++ #include "content.h" %>
<% skin my_app %>
<% view index uses content::index %>
<% template render()%>
<html>
<body>
<h1><%= message %> World!</h1>
</body>
</html>
<% end template %>
<% end view %>
<% end skin %>
content.h
#include <cppcms/view.h>
namespace content{
struct index : public cppcms::base_content{
std::string message;
};
}
我的配置中缺少什么?为什么 CppCMS-Embedded 没有为 Web 应用程序根路由。
谢谢。
在 CppCms 社区中的一些话题之后,Artyom Beilis 对这个问题打开了我的思路。
一直以来我都像 http://domain.com/ 那样请求根 url,我收到 404 错误,因为 CppCms 总是期望至少有一些不为空的映射,比如“/”或任何其他字符串以 "/" 开头。正确的方法始终是映射到以“/”开头的内容。
Artyom Beilis 提出的一个很好的解决方案是让 url 的重写保持与教程相同的形式:How to run the application at the root of the web server。并添加一个与默认 url 匹配的非空映射,例如:
dispatcher().assign("/",&my_app::well_come,this);
mapper().assign("well_come","/");
这个解决方案对我来说效果很好 =;)
我正在尝试使用 CppCms 的一个简单示例,我想执行与 Web 应用程序根相关的所有应用程序。我不想执行相对于脚本路径的应用程序。 Ex localhost: 8080/script-path/relative-path-to-my-application,而不是这个,我希望我的应用程序的路径是这样的:localhost: 8080/relative-path-to-my-application。我想使用 CppCMS-Embedded 来执行这个应用程序。我正在尝试一个超级简单的例子,但没有成功。我一直尝试根 url (http://localhost:8080/) 我得到这样的 404 错误:
Connection close
Content-Encoding gzip
Content-Type text/html; charset=utf-8
Server CppCMS-Embedded/1.1.0
X-Powered-By CppCMS/1.1.0
status 404 Not Found
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control max-age=0
Connection keep-alive
Cookie _ga=GA1.1.541474771.1454701631
Host localhost:8080
User-Agent Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0
下面是我制作的代码和配置文件:
main.cpp:
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <iostream>
#include "content.h"
class my_app : public cppcms::application{
public:
my_app(cppcms::service& s) : cppcms::application(s){
dispatcher().assign("",&my_app::well_come,this);
mapper().assign("");
mapper().root("");
}
void well_come(){
content::index ci;
ci.message = "Hello ";
render("index",ci);
}
};
int main(int argc,char ** argv){
try{
cppcms::service srv(argc,argv);
srv.applications_pool().mount(
cppcms::applications_factory<my_app>()
);
srv.run();
}catch(std::exception const & e){
std::cerr<<e.what()<<std::endl;
}
}
config.js:
{
"http" : {
"script" : "/mb.fcgi",
"rewrite" : [
{ "regex" : "/media(/.*)?", "pattern" : "[=12=]" },
{ "regex" : ".*" , "pattern" : "/mb.fcgi[=12=]" }
]
},
"service": {
"api":"http",
"port":8080
},
"views" : {
"paths" : [ "./" ],
"skins" : [ "my_app"],
},
}
index.tmpl
<% c++ #include "content.h" %>
<% skin my_app %>
<% view index uses content::index %>
<% template render()%>
<html>
<body>
<h1><%= message %> World!</h1>
</body>
</html>
<% end template %>
<% end view %>
<% end skin %>
content.h
#include <cppcms/view.h>
namespace content{
struct index : public cppcms::base_content{
std::string message;
};
}
我的配置中缺少什么?为什么 CppCMS-Embedded 没有为 Web 应用程序根路由。 谢谢。
在 CppCms 社区中的一些话题之后,Artyom Beilis 对这个问题打开了我的思路。 一直以来我都像 http://domain.com/ 那样请求根 url,我收到 404 错误,因为 CppCms 总是期望至少有一些不为空的映射,比如“/”或任何其他字符串以 "/" 开头。正确的方法始终是映射到以“/”开头的内容。 Artyom Beilis 提出的一个很好的解决方案是让 url 的重写保持与教程相同的形式:How to run the application at the root of the web server。并添加一个与默认 url 匹配的非空映射,例如:
dispatcher().assign("/",&my_app::well_come,this);
mapper().assign("well_come","/");
这个解决方案对我来说效果很好 =;)