如何服务特定的文件节点服务器

发布于 2025-02-09 05:46:32 字数 601 浏览 1 评论 0原文

让我们说我有一个带有2个文件的目录:c:\ file1 \ index.html和c:\ file1 \ test.html。我正在尝试在特定端口上使用节点JS启动服务器。现在,我使用app.use('/',express.static(path.join(__ dirname)))作为服务文件的目录。我的问题是,如何使用我拥有的代码来使其服务c:\\ file1 \ test.html,它可以使用c:\\ file1 \ index.html ?提前致谢。

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

//loads index.html
app.use('/', express.static(path.join(__dirname)))

app.listen(3000, () => console.log('Server up on port 3000'))

Let us say I have a directory with 2 files: c:\file1\index.html and c:\file1\test.html. I am trying to start a server with node js on a specific port. Now I used app.use('/', express.static(path.join(__dirname))) as the directory to serve the file. My question is how can I make it serve c:\\file1\test.html with the code I have right now it serves c:\\file1\index.html? Thanks in advance.

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

//loads index.html
app.use('/', express.static(path.join(__dirname)))

app.listen(3000, () => console.log('Server up on port 3000'))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2025-02-16 05:46:33

这个适合您吗?

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

//specify public folder
app.use(express.static('file1'));

app.listen(3000, () => console.log('Server up on port 3000'));

Would this work for you?

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

//specify public folder
app.use(express.static('file1'));

app.listen(3000, () => console.log('Server up on port 3000'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文