简体   繁体   English

NodeJS-尝试呈现index.ejs文件,但仍然仅显示我的公用文件夹中的服务器index.html

[英]NodeJS - Trying to render index.ejs file, but still only servers index.html that is in my public folder

The code below still serves the index.html page that's in the public folder, but I want to render index.ejs . 下面的代码仍然提供了index.html页这是在public文件夹,但我想呈现index.ejs I followed the video example so I'm not sure why he's able to render the ejs file whereas I'm still only seeing the index.html file. 我按照视频示例进行操作,因此我不确定他为什么能够呈现ejs文件,而我仍然只能看到index.html文件。

The video example I am using seems to be the same. 我使用的视频示例似乎相同。 My package.json file has "main": "app.js" . 我的package.json文件具有"main": "app.js"

Here is the video example link, and code below: https://youtu.be/gnsO8-xJ8rs?t=28m12s 这是视频示例链接,以及下面的代码: https : //youtu.be/gnsO8-xJ8rs?t=28m12s

//View Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

//Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : false}));

//Set Static path
app.use(express.static(path.join(__dirname, 'public')));

//Routes
app.get('/', (req, res) => {//homepage represented by the single slash
  res.render('index');
});

app.listen(3000, () => {
  console.log('server started on port 3000...');
});

I noticed that if I comment out 我注意到如果我注释掉

//Routes
app.get('/', (req, res) => {//homepage represented by the single slash
  res.render('index');
});

then the ejs file is rendered and served. 然后呈现并提供ejs文件。

Any advice would be appreciated! 任何意见,将不胜感激!

Note that EJS by default looks into ' Views ' folder 请注意,默认情况下,EJS会查看视图 ”文件夹

app.set('views', path.join(__dirname, 'views'));

You can specify the file path to look in your folder as Views change this to your public folder and It will be up and running. 您可以指定文件路径看你的文件夹中的视图更改为公共文件夹,这将是启动和运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM