app.get('/')在网站打开时没有被调用

问题描述 投票:0回答:2

每当我打开我的网站(即http://127.0.0.1:8090)时,都会发出GET请求。

app.use(session({
  //session stuff
}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('client'));

app.get('/', async function(req, res){
  console.log(req);
}

module.exports = app;
app.listen(8090);

这没有被调用,我不确定为什么 - 我该怎么做才能解决这个问题?打开相关页面时,将调用我的其他app.get()函数。

node.js express
2个回答
0
投票

当您打开您的站点(即http://127.0.0.1:8090)时,它会发送一个GET请求,但不会向浏览器发回任何响应。这就是为什么似乎没有提出GET请求。在app.get中发送回复,它会发送回复。

app.get('/', async function(req, res){
  console.log(req);
  res.send('Hello World');
}

0
投票

express.static('client')建议从哪里加载静态文件。这里'客户'被视为您的根路径。

如果你的'client'目录有一些'abcd.img'文件,那么http://127.0.0.1:8090/abcd.img将加载'abcd.img'。当您指向根路径时,默认情况下将加载“客户端”目录中的“index.html”。这意味着'http://127.0.0.1:8090/'将加载您的index.html文件。

Express在这方面有很好的文档。我正在粘贴它供你参考。 Express documentation for serving static files

© www.soinside.com 2019 - 2024. All rights reserved.