Express Https代码:html文件在哪里提供?
对不起,我有一个愚蠢的问题。下面的代码可以很好地为https文件服务,我很高兴地说,为Express Https代码:html文件在哪里提供?,express,Express,对不起,我有一个愚蠢的问题。下面的代码可以很好地为https文件服务,我很高兴地说,为Index.html文件服务没有问题,但我是新手,我不知道在代码Index.js中服务器知道如何为Index.html文件服务 代码如下: 服务器(Index.js) 客户端(提供给客户端浏览器的Index.html文件所在的目录)Index.html: <!DOCTYPE html> <html lang="en"> <head> <meta
Index.html
文件服务没有问题,但我是新手,我不知道在代码Index.js
中服务器知道如何为Index.html
文件服务
代码如下:
服务器(Index.js
)
客户端(提供给客户端浏览器的Index.html
文件所在的目录)Index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<title>HTTPS App</title>
</head>
<body>
<h1>HTTPS App</h1>
</body>
</html>
package.json
{
"name": "https-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
我很困惑,因为代码中根本没有提到Index.html
。为了测试,我在客户机文件夹中添加了另一个文件(Index2.html
),但代码忽略了该文件。我是否可以假设Express会自动在静态
目录中查找名为Index.html
的文件 查看这一行:
app.use('/',express.static(path.join(__dirname,'..',directoryToServe)));
app.use('/',express.static(path.join(__dirname,'..',directoryToServe)));
您处于
server
文件夹中,然后后退一步进入client
文件夹。默认情况下,要提供的文件是index.html
您正在告诉路径,在这一行中查找index.html
文件。如果文件名不是索引
,则必须告诉文件名和路径
app.use('/',express.static(path.join(__dirname,'..',directoryToServe)));
app.use('/',express.static(path.join(__dirname,'..',directoryToServe)));