如何在Node.js中正确设置路由器

问题描述 投票:1回答:1

当我通过Express运行服务器时,发现了如下所述的错误。

当我访问下面的网址时,

https://5a75307007e3415f9aaca9d3052ed731.vfs.cloud9.us-east-2.amazonaws.com/_static/login_form/views/login

显示以下错误。

The file '/login_form/views/login' could not be found. Are you sure it is in your environment?

我当前的工作如下,

有什么奇怪的地方吗?

谢谢

login_form/views $ ls
login.html  main.css  signup.html
const express = require("express");
const app = express();

app.get('/login', (req, res) => {
  res.render('login.html');
});

app.get('/signup', (req, res) => {
  res.render('signup.html');
});



app.listen(3000,()=>{
    console.log('server is running!');
});
node.js express
1个回答
0
投票

[当您尝试访问页面时,您正在使用系统上的文件路径。您实际上需要的是服务器的网址:

http://localhost:3000(确保您之前已启动节点服务器)

欢呼!


0
投票

[渲染文件供视图引擎使用,例如EJS,Pug等

因此,如果您要使用其他HTML文件,则视图引擎必须使用sendFile()

喜欢这样

app.get('/login', (req, res) => {
  res.sendFile(path.join(__dirname+'/login.html'));
});
© www.soinside.com 2019 - 2024. All rights reserved.