简体   繁体   English

如何使用 express.js 发送另一个文件夹中的文件?

[英]how can I send a file that's inside another folder using express.js?

why is my browser says that the folder and the folder I wanted to send are don't exist?为什么我的浏览器提示我要发送的文件夹和文件夹不存在?

const express = require('express');
const app = express();
const port = 3000 || process.env.PORT;

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/Structures/index.html');
})

app.listen(port, () => {
    console.log('listening on port' + port);
});

check the path of index.html com your foldering should be like this:检查 index.html com 的路径,你的文件夹应该是这样的:

在此处输入图像描述

if your foldering like this:如果你的文件夹是这样的:

在此处输入图像描述

user path module and join path so just try用户path模块和join路径,所以试试

const path = require('path');
const express = require('express');
const app = express();
const port = 3000 || process.env.PORT;

app.get('/', (req, res) => {
    // res.sendFile(__dirname + '../Structures/index.html');
    res.sendFile(path.join(__dirname,'../Structures/index.html')) 
})

app.listen(port, () => {
    console.log('listening on port' + port);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用express.js在html中显示文件夹? - How can i display folder in html using express.js? 如何在 express.js 中设置使用 response.send () 发送的数据的样式 - How can I style the data sent using response.send () in express.js 如何访问 MongoDB 中的图像文件并使用 Express.js 将它们发送到客户端 - How can I access image files in MongoDB and send them to the client using Express.js 如何在express.js中提供默认图像文件? - How can I serve a default image file in express.js? 如何在Express.js中将JS字符串作为xml文档发送给用户? - How can I send JS string as xml document to user in Express.js? 如何从路由器发送请求参数? Express.js - How can I send a request parameter from a router? Express.js 如何使用ajax和express.js发送和接收字符串? - How do I send and receive a string using ajax and express.js? 如何使用express.js在另一个MySQL查询中执行mySQL查询 - How can I execute an mySQL query in another MySQL query with express.js 如何使用 Express.js 在内部链接到另一个 HTML 模板引擎页面? - How do I internally link to another HTMLtemplating engine page using Express.js? 如何使用 express.js 在 socket.io 的响应中过度添加自定义标头? - How can I over add custom headers to socket.io ' s response with express.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM