I am new to nodeJS and I try to create some simple blogs using Nodejs,express and mongodb. I faces problem as when i try access other page in blog website it show error Cannot GET /index.ejs
. Other pages also show the same thing.I try to fix it but I cant seem to find the solution. I tried change the code<a href="post.ejs">
to <%- include('views/post'); -%>
but it become error to the whole website.
This is my code for index.ejs
<!DOCTYPE html>
<html lang="en">
<%- include('layouts/header'); %>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Clean Blog - Start Bootstrap Theme</title>
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Font Awesome icons (free version)-->
<script src="https://use.fontawesome.com/releases/v6.1.0/js/all.js" crossorigin="anonymous"></script>
<!-- Google fonts-->
<link href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Open Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
</head>
<body>
<!-- Navigation-->
<nav id="mainNav">
<div >
<a id="index"><a href="index.ejs">Start Bootsrap</a>
<button type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
Menu
<i ></i>
<%- include('layouts/navbar'); -%>
<!-- Page Header-->
<header style="background-image: url('assets/img/home-bg.jpg')">
<div >
<div >
<div >
<div >
<h1>Clean Blog</h1>
<span >A Blog Theme by Start Bootstrap</span>
</div>
</div>
</div>
</div>
</header>
<!-- Main Content-->
<div >
<div >
<div >
<!-- Post preview-->
<div >
<a href="post.ejs">
<h2 >Man must explore, and this is exploration at its greatest</h2>
<h3 >Problems look mighty small from 150 miles up</h3>
</a>
<p >
Posted by
<a href="#!">Start Bootstrap</a>
on September 24, 2022
</p>
</div>
<!-- Divider-->
<hr />
<!-- Post preview-->
<div >
**<a href="post.ejs"><h2 >I believe every human has a finite number of heartbeats. I don't intend to waste any of mine.</h2></a>**
<p >
Posted by
<a href="#!">Start Bootstrap</a>
on September 18, 2022
</p>
</div>
<!-- Divider-->
<hr />
<!-- Post preview-->
<div >
<a href="post.ejs">
<h2 >Science has not yet mastered prophecy</h2>
<h3 >We predict too much for the next year and yet far too little for the next ten.</h3>
</a>
<p >
Posted by
<a href="#!">Start Bootstrap</a>
on August 24, 2022
</p>
</div>
<!-- Divider-->
<hr />
<!-- Post preview-->
<div >
<a href="post.ejs">
<h2 >Failure is not an option</h2>
<h3 >Many say exploration is part of our destiny, but it’s actually our duty to future generations.</h3>
</a>
<p >
Posted by
<a href="#!">Start Bootstrap</a>
on July 8, 2022
</p>
</div>
<!-- Divider-->
<hr />
<!-- Pager-->
<div ><a href="#!">Older Posts →</a></div>
</div>
</div>
</div>
<!-- Footer-->
<footer >
<div >
<div >
<div >
<ul >
<li >
<a href="#!">
<span >
<i ></i>
<i ></i>
</span>
</a>
</li>
<li >
<a href="#!">
<span >
<i ></i>
<i ></i>
</span>
</a>
</li>
<li >
<a href="#!">
<span >
<i ></i>
<i ></i>
</span>
</a>
</li>
</ul>
<div >Copyright © Your Website 2022</div>
</div>
</div>
</div>
<%- include('layouts/footer'); -%>
<%- include('layouts/scripts'); -%>
</footer>
<!-- Bootstrap core JS-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Core theme JS-->
<script src="js/scripts.js"></script>
</body>
</html>
I also already set the index.js according ejs.
const express=require('express')
const path=require('path')
const app=new express()
const ejs=require('ejs')
app.set('view engine','ejs')
app.use(express.static('public'))
app.listen(4000,()=>{
console.log('App listening on port 4000')
})
app.get('/',(req,res)=>{
res.render('index.ejs')
})
app.get('./about',(req,res)=>{
//res.sendFile(path.resolve(__dirname,'pages/about.ejs')
res.render('about')
})
app.get('./contact',(req,res)=>{
// res.sendFile(path.resolve(__dirname,'pages/contact.ejs'))
res.render('contact')
})
app.get('./post',(req,res)=>{
//res.sendFile(path.resolve(__dirname,'pages/post.ejs'))
res.render('post')
})
const mongoose=require('mongoose');
mongoose.connect('mongodb://localhost/my_database',{useNewUrlParser:true})
This is other pages for post.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Clean Blog - Start Bootstrap Theme</title>
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Font Awesome icons (free version)-->
<script src="https://use.fontawesome.com/releases/v6.1.0/js/all.js" crossorigin="anonymous"></script>
<!-- Google fonts-->
<link href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Open Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
</head>
<body>
<!-- Navigation-->
<nav id="mainNav">
<div >
<a href="index.ejs">Start Bootstrap</a>
<button type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
Menu
<i ></i>
<!-- Page Header-->
<header style="background-image: url('assets/img/post-bg.jpg')">
<div >
<div >
<div >
<div >
<h1>Man must explore, and this is exploration at its greatest</h1>
<h2 >Problems look mighty small from 150 miles up</h2>
<span >
Posted by
<a href="#!">Start Bootstrap</a>
on August 24, 2022
</span>
</div>
</div>
</div>
</div>
</header>
<!-- Post Content-->
<article >
<div >
<div >
<div >
<p>Never in all their history have men been able truly to conceive of the world as one: a single sphere, a globe, having the qualities of a globe, a round earth in which all the directions eventually meet, in which there is no center because every point, or none, is center — an equal earth which all men occupy as equals. The airman's earth, if free men make it, will be truly round: a globe in practice, not in theory.</p>
<p>Science cuts two ways, of course; its products can be used for both good and evil. But there's no turning back from science. The early warnings about technological dangers also come from science.</p>
<p>What was most significant about the lunar voyage was not that man set foot on the Moon but that they set eye on the earth.</p>
<p>A Chinese tale tells of some men sent to harm a young girl who, upon seeing her beauty, become her protectors rather than her violators. That's how I felt seeing the Earth for the first time. I could not help but love and cherish her.</p>
<p>For those who have seen the Earth from space, and for the hundreds and perhaps thousands more who will, the experience most certainly changes your perspective. The things that we share in our world are far more valuable than those which divide us.</p>
<h2 >The Final Frontier</h2>
<p>There can be no thought of finishing for ‘aiming for the stars.’ Both figuratively and literally, it is a task to occupy the generations. And no matter how much progress one makes, there is always the thrill of just beginning.</p>
<p>There can be no thought of finishing for ‘aiming for the stars.’ Both figuratively and literally, it is a task to occupy the generations. And no matter how much progress one makes, there is always the thrill of just beginning.</p>
<blockquote >The dreams of yesterday are the hopes of today and the reality of tomorrow. Science has not yet mastered prophecy. We predict too much for the next year and yet far too little for the next ten.</blockquote>
<p>Spaceflights cannot be stopped. This is not the work of any one man or even a group of men. It is a historical process which mankind is carrying out in accordance with the natural laws of human development.</p>
<h2 >Reaching for the Stars</h2>
<p>As we got further and further away, it [the Earth] diminished in size. Finally it shrank to the size of a marble, the most beautiful you can imagine. That beautiful, warm, living object looked so fragile, so delicate, that if you touched it with a finger it would crumble and fall apart. Seeing this has to change a man.</p>
<a href="#!"><img src="assets/img/post-sample-image.jpg" alt="..." /></a>
<span >To go places and do things that have never been done before – that’s what living is all about.</span>
<p>Space, the final frontier. These are the voyages of the Starship Enterprise. Its five-year mission: to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no man has gone before.</p>
<p>As I stand out here in the wonders of the unknown at Hadley, I sort of realize there’s a fundamental truth to our nature, Man must explore, and this is exploration at its greatest.</p>
<p>
Placeholder text by
<a href="http://spaceipsum.com/">Space Ipsum</a>
· Images by
<a href="https://www.flickr.com/photos/nasacommons/">NASA on The Commons</a>
</p>
</div>
</div>
</div>
</article>
<!-- Footer-->
<footer >
<div >
<div >
<div >
<ul >
<li >
<a href="#!">
<span >
<i ></i>
<i ></i>
</span>
</a>
</li>
<li >
<a href="#!">
<span >
<i ></i>
<i ></i>
</span>
</a>
</li>
<li >
<a href="#!">
<span >
<i ></i>
<i ></i>
</span>
</a>
</li>
</ul>
<div >Copyright © Your Website 2022</div>
</div>
</div>
</div>
</footer>
<!-- Bootstrap core JS-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Core theme JS-->
<script src="js/scripts.js"></script>
</body>
</html>
This my package.json
{
"name": "blog",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"ejs": "^3.1.8",
"express": "^4.18.2",
"mongoose": "^6.6.5"
},
"devDependencies": {
"nodemon": "^2.0.20"
}
}
CodePudding user response:
I have tested your codes, after using the correct syntax for routes, all should be working fine.
// the route './about' is incorrect, please change the rest.
app.get('/about',(req,res)=>{
res.render('about')
});
And also check all the href. It should link to route or url.
<a href="/about">