首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在uWebSockets HTTP服务器(C++)中提供静态文件

在uWebSockets HTTP服务器(C++)中提供静态文件
EN

Stack Overflow用户
提问于 2022-09-08 12:57:52
回答 2查看 202关注 0票数 0

我正在使用C++库在uWebSockets中设置一个HTTP服务器,我希望添加一个中间件来服务静态文件,类似于app.use(express.static(path.join(__dirname, 'public')));在Express.js中所做的工作。

静态文件驻留在public文件夹中。中间件应该在path http://localhost:8087/css/bootstrap.min.css而不是http://localhost:8087/public/css/bootstrap.min.css下加载服务器文件,从而将root重新路由到public

如何使用C++库在uWebSockets中做到这一点?我已经检查了uWS::App结构,但是我发现没有任何与路径或服务静态文件相关的内容。

下面是一个HTTP服务器的示例:

代码语言:javascript
复制
#include <uWebSockets/App.h>
#include <rapidjson/rapidjson.h>

#include "util/AsyncFileReader.h"
#include "util/AsyncFileStreamer.h"

#include <iostream>
#include <string>



void get_home(uWS::HttpResponse<false> *res, uWS::HttpRequest *req) {
  res->writeHeader("Content-Type", "text/html; charset=utf8");
  // res->writeStatus(uWS::HTTP_200_OK);
  // res->end("Hello! This is <b>Sergei's C++ web server</b>.");
  AsyncFileReader page_contents("./public/home.html");
  res->end(page_contents.peek(0));
}

int main() {
  int port{8087};

  // HTTP
  uWS::App app = uWS::App();
  app.get("/", get_home);


  app.listen(port, [&port](auto *token) {
    if (token) {
      std::cout << "Listening on port " << port << std::endl;
    }
  })
  .run();

  return 0;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-09-11 18:26:19

这里有一个例子

最后,我添加了一个目录监视,并更新了html文件,如果保存(代码库中的一些变化),但我想这是另一回事。

代码语言:javascript
复制
#include "helpers/AsyncFileReader.h"
#include "helpers/AsyncFileStreamer.h"
#include "helpers/Middleware.h"

AsyncFileStreamer asyncFileStreamer("htmls"); // htmls is a relative folder path to static files
代码语言:javascript
复制
app.get("/*", gethome); // note the *
代码语言:javascript
复制
void get_home(auto *res, auto *req) {
//void get_home(uWS::HttpResponse<false> *res, uWS::HttpRequest *req) {
  
  serveFile(res, req); // essentially res->writeStatus(uWS::HTTP_200_OK);
  asyncFileStreamer.streamFile(res, req->getUrl());
}

请注意,serveFile()函数还需要处理图像的不同内容类型标题设置

提到的例子:https://github.com/uNetworking/uWebSockets/blob/master/examples/HttpServer.cpp

票数 1
EN

Stack Overflow用户

发布于 2022-09-18 11:53:18

为了扩展@amit.user105387的答案,下面是一个可行的解决方案的样子:

代码语言:javascript
复制
#include <uWebSockets/App.h>
#include <rapidjson/rapidjson.h>

#include "util/AsyncFileReader.h"
#include "util/AsyncFileStreamer.h"
#include "util/Middleware.h"

#include <iostream>
#include <string>

void get_home(uWS::HttpResponse<false> *res, uWS::HttpRequest *req) {
  // res->writeHeader("Alt-Svc", "h2=\"localhost:8087\"public\"");
  res->writeHeader("Content-Type", "text/html; charset=utf8");
  AsyncFileReader page_contents("./public/html/home.html");
  res->end(page_contents.peek(0));
}

void PrintCachedFiles(std::map<std::string_view, AsyncFileReader *> dict) {
  for (auto it = dict.begin(); it != dict.end(); it++) {
    std::cout << it->first << std::endl;
  }
}

int main() {
  const int port{8087};
  const std::string public_folder{"./public"};
  AsyncFileStreamer public_files("./public");

  // HTTP
  uWS::App app = uWS::App();

  app.get("/", [&public_files](auto res, auto req){
    std::cout << "root folder = "<< public_files.root << std::endl;
    PrintCachedFiles(public_files.asyncFileReaders);
    res->writeHeader("Content-Type", "text/html; charset=utf8");
    public_files.streamFile(res, "/html/home.html");
    res->writeStatus(uWS::HTTP_200_OK)->end();
  });

  app.get("/css/:path", [&public_files](auto res, auto req){
    public_files.streamFile(res, req->getUrl());
    res->writeStatus(uWS::HTTP_200_OK)->end();
  });

  app.get("/js/:path", [&public_files](auto res, auto req){
    public_files.streamFile(res, req->getUrl());
    res->writeStatus(uWS::HTTP_200_OK)->end();
  });

  app.get("/img/:path/*", [&public_files](auto res, auto req){
    serveFile(res, req);
    public_files.streamFile(res, req->getUrl());
    res->writeStatus(uWS::HTTP_200_OK)->end();
  });

  app.get("/*", [&public_files](auto res, auto req){    
    res->writeStatus(uWS::HTTP_200_OK)->end("404: Page not found.");
  });


  app.listen(port, [&port](auto *token) {
    if (token) {
      std::cout << "Listening on port " << port << std::endl;
    }
  })
  .run();

  return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73649475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档