I make an app with javascript, I need to work with no any server in the local network. Only chrome browser run from /Services/ServiceFinder/index.html.
When load index.html need to read a XLSX file from the same directory as a file(i use the SheetJS library to read XLSX as a db and convert it to a javascript object).
I use fetch after window load event but i take an error message "The file is not loaded successfully."
window.addEventListener('load', (event) => {
//Get the file input
var file = "./ServicesData.XLSX";
//Load file
fetch(file, {mode: "no-cors"}).then(response => {
if(response.ok) {
console.log("%cThe file is loaded successfully.", styleConsoleLog);
}else{
console.error("The file is not loaded successfully.");
};
return response.arrayBuffer();
}).then(arrayBuffer => {
The same code is working perfectly when i use the server.
You can simply create a http server on the local network and serve the file there with NodeJS.
/* ====== create node.js server with core 'http' module ====== */
// dependencies
const http = require("http");
// PORT
const PORT = 3000;
// server create
const server = http.createServer((req, res) => {
if (req.url === "/") {
res.write("This is home page.");
res.end();
} else if (req.url === "/about" && req.method === "GET") {
res.write("This is about page.");
res.end();
} else {
res.write("Not Found!");
res.end();
}
});
// server listen port
server.listen(PORT);
console.log(`Server is running on PORT: ${PORT}`);
// ======== Instructions ========
// save this as index.js
// you have to download and install node.js on your machine
// open terminal or command prompt
// type node index.js
// find your server at http://your.network.ip.address:port/
learn about it here: How could others, on a local network, access my NodeJS app while it's running on my machine?