当在nodejsexpress中发送文件时,两个html文件中的内容相同



嗨,对nodejs还是个新手,正在尝试创建一个有两个页面的简单网站。我目前遇到的问题是,第二个文件的内容被渲染为第一个文件,尽管浏览器中的源检查器说它正在查找第二个。

server.js如下:

const http = require('http');
const express = require('express');
const path = require('path');
const app = express();
app.use(express.json());
app.use(express.static("express"));
// default URL for website
app.use('/', function(req,res){
res.sendFile(path.join(__dirname+'/express/index.html'));
//__dirname : It will resolve to your project folder.
});
app.get('/avocado', function(req,res){
res.sendFile(path.join(__dirname+'/express/avocado.html'));
});
const server = http.createServer(app);
const port = 3000;
server.listen(port);
console.debug('Server listening on port ' + port);

而它指向的文件夹"中的另外两个html文件;express";目前只有真正的基本html来尝试轻松调试。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>I start here</p>
</body>
</html>

和鳄梨.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>I Get to this page fine</p>
</body>
</html>

正如所说的,浏览器中的源检查器声称可以访问这两个,但这两个都引用了当定位在/或/鳄梨时会说";我从这里开始">

您已经使用app.use将第一个处理程序设置为中间件

它在到达/avocado的路由之前运行,因此将始终使用索引文件进行响应。

使用app.get作为端点。中间件只能使用app.use

最新更新