Codecygen
Codecygen

Reputation: 121

Page is not Displayed - Cannot GET /store EJS Problem

I have a problem displaying an additional webpage in my Express JS app. I have Node.js and Express installed. The server is running fine but I cannot load a new page that I created. Here is my "server.js" file content. I edited the content of "server.js" file after creating the "/views/pages/store.ejs" file.

// Load Node modules
var express = require('express');
const ejs = require('ejs');
// Initialize Express
var app = express();
// Render static files
app.use(express.static('/home/username/Desktop/www/website.com'));
// Set the view engine to ejs
app.set('view engine', 'ejs');
// Port website will run on
app.listen(1253);
// *** GET Routes - Display Pages ***
// Root Route
app.get('/', function(req, res){
    // Render index page
    res.render('/home/username/Desktop/www/website.com/views/pages/index.ejs', {
        // EJS variable and server side variable
    });
    
    res.render('/home/username/Desktop/www/website.com/views/pages/store.ejs', {
        // EJS variable and server side variable
    });
});

And then, when I try to put an "href" in the "index.ejs" file:

<a href="store">Store</a>

It says "Cannot GET /store". I restarted server and did some other tweaks but it does not seem like working.

I also get this error message when I click on the link:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/home/username/Desktop/www/website.com/node_modules/express/lib/response.js:771:10)
    at ServerResponse.contentType (/home/username/Desktop/www/website.com/node_modules/express/lib/response.js:599:15)
    at ServerResponse.send (/home/username/Desktop/www/website.com/node_modules/express/lib/response.js:145:14)
    at done (/home/username/Desktop/www/website.com/node_modules/express/lib/response.js:1008:10)
    at tryHandleCache (/home/username/Desktop/www/website.com/node_modules/ejs/lib/ejs.js:278:5)
    at View.exports.renderFile [as engine] (/home/username/Desktop/www/website.com/node_modules/ejs/lib/ejs.js:489:10)
    at View.render (/home/username/Desktop/www/website.com/node_modules/express/lib/view.js:135:8)
    at tryRender (/home/username/Desktop/www/website.com/node_modules/express/lib/application.js:640:10)
    at Function.render (/home/username/Desktop/www/website.com/node_modules/express/lib/application.js:592:3)

Any help would be appreciated.

Upvotes: 1

Views: 696

Answers (1)

Viral Patel
Viral Patel

Reputation: 1166

Two issues with your current code.

  1. You're getting this Cannot GET /store error because you don't have route for /store.
  2. You're getting this Cannot set headers after they are... error because you're sending the output twice (you have written res.render twice within / route).

You should have following code.

// Root Route
app.get('/', function(req, res){
    // Render index page
    res.render('/home/username/Desktop/www/website.com/views/pages/index.ejs', {
        // EJS variable and server side variable
    });
})

// Store Route
app.get('/store', function(req, res){
    // Render store page
    res.render('/home/username/Desktop/www/website.com/views/pages/store.ejs', {
        // EJS variable and server side variable
    });
});

Upvotes: 1

Related Questions