【问题标题】:How can I get html file from another folder with express?如何使用 express 从另一个文件夹中获取 html 文件?
【发布时间】:2021-11-27 16:40:17
【问题描述】:

目前我在同一个文件夹中使用 html 文件。那么如何使用 express 获取保存在不同文件夹中的 html 文件。谢谢。

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

app.get('/',function(req,res){
res.sendFile(path.join(__dirname,'index.html'))
})

app.get('/about',function(req,res){
    res.send('about')
})

app.listen(3000,function(){
    console.log('Server Online')
    
})

【问题讨论】:

  • console.log(__dirname); 看看。例如,您可以使用path.join(__dirname, "html/index.html");

标签: javascript node.js express web-development-server


【解决方案1】:

对于给定的 url,返回文件的路径

app.get('/yourUrl',function(req,res){
res.sendFile("path/to/your/file.html"))
})

在您的具体情况下尝试 about.html 而不是 about?

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 2011-06-20
    • 2021-04-24
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多