首页 > nodejs 小白求问静态文件读取的路径问题

nodejs 小白求问静态文件读取的路径问题

var express = require("express");

var app = express();
app.use(express.static('public'));
app.listen(3000);

public文件夹下有个index.html
http://127.0.0.1:3000/index.html
http://127.0.0.1:3000/
这样都能直接打开index.html
然而
http://127.0.0.1:3000/public/...
这样输入反而是错误的.
这样有什么区别吗?

还有看教程 明明有app.use(express.static('public'));就可以访问静态html了,教程上
还加

app.get('/index.html',function(req,res){
    res.sendFile(__dirname+"/index.html");
});

请问这么做有什么意义吗?


express中,app.use(express.static('public'))完成了两个步骤:

1.指定静态资源根路径为public
2.默认静态资源访问路径为'/'

你可以通过以下方式自定义访问地址:

app.use('/a', express.static('a'));
app.use('/b', express.static('b'));

可以参考文档:http://expressjs.com/en/starter/static-files.html


app.use(express.static('public'));

是中间件,来指定静态文件的路径

【热门文章】
【热门文章】