最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I send multiple files with expressjs? - Stack Overflow

programmeradmin0浏览0评论

I want to be able to send many files, and if possible the a whole directory so that I can access it in my other js file that is getting called from an html file.

const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 3000;

app.use("/styles", express.static(__dirname));

app.get("/", function (req, res) {
  //res.sendFile("C:/Users/Kevin_Who/Desktop/Online Chat Game/index.html");
  //res.sendFile("C:/Users/Kevin_Who/Desktop/Online Chat Game/mainScript.js");
  res.sendFile("C:/Users/Kevin_Who/Desktop/Online Game Files/");
  //app.use(express.static("Online Game Files"));
});

I tried sending many files with res.sendFile, but it only sends the first one. In the html file it references mainScript.js inside a script tag (<script src="mainScript.js" type="module"></script>). Inside the mainScript.js it also references image files, and I need help finding how to send and access these files(tiles[2].src = "./Assets/Blocks/StoneBrickWall.png";).

I want to be able to send many files, and if possible the a whole directory so that I can access it in my other js file that is getting called from an html file.

const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 3000;

app.use("/styles", express.static(__dirname));

app.get("/", function (req, res) {
  //res.sendFile("C:/Users/Kevin_Who/Desktop/Online Chat Game/index.html");
  //res.sendFile("C:/Users/Kevin_Who/Desktop/Online Chat Game/mainScript.js");
  res.sendFile("C:/Users/Kevin_Who/Desktop/Online Game Files/");
  //app.use(express.static("Online Game Files"));
});

I tried sending many files with res.sendFile, but it only sends the first one. In the html file it references mainScript.js inside a script tag (<script src="mainScript.js" type="module"></script>). Inside the mainScript.js it also references image files, and I need help finding how to send and access these files(tiles[2].src = "./Assets/Blocks/StoneBrickWall.png";).

Share Improve this question asked Jun 27, 2020 at 21:21 Kevin WhoKevin Who 431 gold badge1 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

When the browser gets to this in your html file:

<script src="mainScript.js" type="module"></script>

It will create a NEW http request to your server. That http request will be to <somePath>/mainScript.js where <somePath> is the path of the current page URL. If that URL is a top level URL, then the request will go to /mainScript.js. Your server needs to handler that specific request for /mainScript.js and send the appropriate script file.

This is how web pages work. The browser makes a separate http request for each resource in the file (scripts, images, CSS files, etc...) and your server needs to separately respond to each of those http requests and send the appropriate resource.

In Express, these requests for static resources are typically handled with express.static() where one line of middleware can handle the serving of many files within a directory hierarchy.

Note: It is generally advisable not to use path relative URLs for your static resources because the browser will bine those path relative URLs with the path of the current web page and this will make it hard to use the same static resources in different web pages on your site. Instead, start your static URLs with a leading / so they generate a constant URL request to your server no matter which page they are used in.

I tried sending many files with res.sendFile

You can only send one file with res.sendFile(). It sends that one file as the http response and you can only send one http response per request.

Inside the mainScript.js it also references image files, and I need help finding how to send and access these files tiles[2].src = "./Assets/Blocks/StoneBrickWall.png";

This will also cause the browser to make a new request to your server that you will also need to have your server respond to. Again, you probably don't want it to start with ./, but rather just /.

You can zip your files and then it'll be as if you're sending only one of them.

const express = require('express');
const app = express();

app.get('/download', (req, res) => {
  const file1Path = '/path/to/your/file1'; // Replace with your file 1 path
  const file2Path = '/path/to/your/file2'; // Replace with your file 2 path

  const files = [file1Path, file2Path];
  const archiveName = 'your-archive-name.zip'; // Replace with your archive name

  res.set('Content-Type', 'application/zip');
  res.set('Content-Disposition', `attachment; filename=${archiveName}`);

  res.zip(files, archiveName, (err) => {
    if (err) {
      console.log('Error sending files:', err);
    } else {
      console.log('Files sent successfully');
    }
  });
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});
发布评论

评论列表(0)

  1. 暂无评论