我想让我的news.ejs页面在单击链接时呈现,但是出现一个错误,提示“无法获取/news.ejs”
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.get('/news', (req, res) => res.render('pages/news'))
.listen(PORT, () => console.log(`Listening on ${ PORT }`))
答案 0 :(得分:2)
我有这段代码并且工作正常
const express = require('express');
const app = express()
const path = require('path')
const ejs = require('ejs');
app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', ejs)
app.get('/', (req, res) => res.render('privacypolicy.ejs'))
app.get('/news', (req, res) => res.render('rulesnregulations.ejs'))
//app.use('/', indexRouter)
const PORT = 5000;
app.listen(PORT, () => console.log('it started on 5000'))
privacypolicy.ejs和rulesnregulations.ejs这两个文件位于视图文件夹
下答案 1 :(得分:-1)
确保您链接到/news
,而不是/news.ejs
。如果您希望访问/news.ejs
,则必须更改正在注册的路径。