我想在 esp32 微控制器上的 Web 服務器上部署一個反應應用程式,以控制同一個微控制器上的 api。
Web 服務器正在作業,可以發送檔案和接收請求。唯一真正的問題是 react 應用程式的檔案名太長(即 ./build/static/js/988.78dc5abd.chunk.js),而 esp32 上的檔案系統僅限于檔案名不超過 31 個字符。
我嘗試通過編輯 webpack.config.js 來減少檔案名,但這似乎不再起作用。我還嘗試將它捆綁在一個我也無法弄清楚的檔案中。增加檔案名限制似乎也是不可能的。
有誰知道我如何在檔案系統上部署一個反應應用程式,該檔案系統僅限于 32 個字符的檔案名?
uj5u.com熱心網友回復:
我為這個問題創建了一個非常糟糕的解決方案,所以如果你看到這篇文章,請確保你在嘗試復制之前用盡了所有其他選項:
基本上,我創建了一個腳本,從 react 應用程式構建目錄(rapp/build)遞回地獲取所有檔案,并將它們全部復制到帶有數字和正確擴展名的資料檔案夾(因此瀏覽器選擇檔案型別):
#!/bin/bash
cd rapp/build
i=0
#clear index and data folder
rm -rf ../../data/*
> ../../data/index
#grab all files and assign number
for f in $(find . -type f -printf '%P\n');
do
#pretty output
RED='\033[0;31m'
NC='\033[0m' # No Color
#grab extension
filename="${f##*/}"
extension="${filename##*.}"
#copy file with number
cp $f "../../data/$i.$extension"
#add original to index
echo $f >> ../../data/index
#add copy to index
echo $i.$extension >> ../../data/index
echo -e $i.$extension ${RED} mapped to ${NC} $f
i=$((i 1))
done
然后我創建了一個 Web 服務器,它將自動將所有請求重定向到復制的編號檔案:
#include "WiFi.h"
#include "SPIFFS.h"
#include "ESPAsyncWebServer.h"
#include <string>
const char* ssid = "abcdef";
const char* password = "";
AsyncWebServer server(80);
void mapRedirect(){
File file = SPIFFS.open("/index");
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.println("Contents of file:");
int i=0;
while (file.available()) {
String orig=file.readStringUntil('\n');
String cop=file.readStringUntil('\n');
Serial.print(cop);
Serial.print("\tmapped to\t");
Serial.println(orig);
server.on(String("/" orig).c_str(), HTTP_GET, [cop](AsyncWebServerRequest *request){
request->redirect("/" String(cop));
}
);
i ;
}
file.close();
}
void setup(){
Serial.begin(115200);
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
WiFi.softAP(ssid,password);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->redirect("/index.html");
});
server.serveStatic("/",SPIFFS,"/");
//redirect react files to coressponding mappings (spiffs character file name limit)
mapRedirect();
server.onNotFound([](AsyncWebServerRequest *request){
request->send(404, "text/plain", "The content you are looking for was not found.");
});
server.begin();
}
void loop(){}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433711.html
