Unlocking the power of setting dynamic meta content

When using EJS for templating in my node.js webserver, everything has been running smoothly except for one issue. After integrating the head file into a page, I encountered the following error:

SyntaxError: Unexpected identifier in /home/runner/superstrap/temp/docs/getting-started/introduction/index.html while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
at new Function (<anonymous>)
at Template.compile (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:673:12)
at Object.compile (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:398:16)
at handleCache (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:235:18)
at tryHandleCache (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:274:16)
at exports.renderFile \[as engine\] (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:491:10)
at View.render (/home/runner/superstrap/node_modules/express/lib/view.js:135:8)
at tryRender (/home/runner/superstrap/node_modules/express/lib/application.js:657:10)
at Function.render (/home/runner/superstrap/node_modules/express/lib/application.js:609:3)
<title><%= title %></title>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="<%= description %>">
<meta name="author" content="gtoy1118, muchtek, and superStrap contributors">
<meta name="generator" content="EJS">

<meta name="docsearch:language" content="en">
<meta name="docsearch:version" content="1.0">

... (content continues)

<em><strong>REMEMBER: There is another page with the same setup just without the head.ejs include that works fine, and this page worked fine without the head.ejs include</strong></em></p>
    </div></questionbody>
<exquestionbody>
<div class="question">
                
<p>To implement EJS templating on my node.js server, I incorporated the head file into a webpage. However, when doing so, I encountered a critical error:</p>
<pre><code>SyntaxError: Unexpected identifier in /home/runner/superstrap/temp/docs/getting-started/introduction/index.html while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
at new Function (<anonymous>)
at Template.compile (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:673:12)
at Object.compile (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:398:16)
at handleCache (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:235:18)
at tryHandleCache (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:274:16)
at exports.renderFile \[as engine\] (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:491:10)
at View.render (/home/runner/superstrap/node_modules/express/lib/view.js:135:8)
at tryRender (/home/runner/superstrap/node_modules/express/lib/application.js:657:10)
at Function.render (/home/runner/superstrap/node_modules/express/lib/application.js:609:3)
<title><%= title %></title>

... (continued HTML content)

<head>
  <%- include("../../../parts/head", {
    title: Getting Started · superStrap v1.0
  }) %>
</head>

The relevant JavaScript code:

const app = require("express")();
const fetch = require("node-fetch");
const ejs = require("ejs");
const port = 8080;

const projects = require("./projects");
const route = require("./route");

app.engine("html", ejs.renderFile);
app.set('view engine', "html");
app.set('views', __dirname + '/temp');

app.use(route.signIn);

app.use(require("express").static(__dirname + "/public"));

app
  .get("/", (req, res) => {
    res.render("index")
  })
  ;

app
  .get("/docs/getting-started/introduction", (req, res) => {
    res.render("docs/getting-started/introduction/index")
  })
  ;

app.use(route[404])

app.listen(port);

console.log("server started at port " + port);

route.js:

module.exports = {
  404: (req, res, next) => res.render("404"),
  signIn: (req, res, next) => {
    if (req.headers["X-Replit-User-Name"]) {
      res.locals.signedIn = false;
    } else {
      res.locals.username = req.headers["X-Replit-User-Name"];
    }
    next();
  }
};

projects.js:

module.exports = "no";

While expecting the title to display as

Getting Started · superStrap v1.0
, unfortunately, attempting to load the html resulted in an error as mentioned above.
REMEMBER: There is another page with the same setup just without the head.ejs include that works fine, and this page worked fine without the head.ejs include

Answer №1

Make sure to correctly quote your HTML:

<body>
  <%- include("../../../parts/body", {
    title: "Getting Started · superStrap v1.0"
  }) %>
</body>

(Remember, in EJS, the code within <%- and %> blocks must be valid JavaScript)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging IOTA MAM for message communication through a node.js server

-I am currently utilizing the repository at this link: https://github.com/xdk2mam/xdk2mam/tree/Workbench-3.6/http-sdcard. -My expertise in JavaScript is limited, so I am struggling to comprehend the issue at hand. -I am attempting to transmit a string ...

Creating a thumbnail with a close button using an image

My code is available at the following link: https://codepen.io/manuchadha/pen/PBKYBJ In this form I have created, I am trying to implement an image upload feature using a file input. The goal is to display a thumbnail of the selected image below the file ...

Exploring the depths of deep populating in Mongo and Node.js

I am currently struggling with a complex data population issue. var commentSchema = mongoose.Schema({ name: String }); var userSchema = mongoose.Schema({ userId: { type: String, default: '' }, comments: [subSchema] }); var soci ...

Shift the "Login link" to the right side of the Bootstrap navbar

Currently working on a Django website and incorporating Bootstrap/CSS/HTML to enhance the design. I've set up a navbar menu and want the login tab on the right side. However, despite my attempts, I'm not achieving the desired outcome: The HTML ...

Does Internet Explorer have a tool similar to Firebug for debugging websites?

Is there a tool similar to Firebug that is compatible with Internet Explorer? Edit I am currently using IE8, so I need a solution that works specifically with IE8. ...

Creating a Cross-Fade Effect in easySlider 1.7 using the Jquery Plugin

Want to achieve a cross-fade effect in easySlider 1.7 Jquery Plugin? Check out this tutorial Easy Slider 1.7 for guidance. Appreciate any help, thanks! ...

Exploring the integration of Stripe Checkout with React and Express.js

Seeking assistance with integrating Stripe Checkout into my React application. I need to create a route in my nodejs/express server that will provide the session id required for setting up Stripe Checkout on the front end. The aim is to redirect users to s ...

Unable to add items to the collection in NPM with Meteor 1.3

I have encountered an issue with the imap-simple NPM package while trying to perform an insert operation. Despite following the suggestions on , I am still unable to get the insert function to work properly! Even after simplifying the code and eliminatin ...

The absence of the 'Access-Control-Allow-Origin' header is detected in the requested resource by Keycloak

We are currently experiencing an issue with accessing our nodejs app from Chrome, which has Keycloak configured. Keycloak version: 21.0.1 When trying to access http://localhost:3101/graphql from Chrome, we encountered the following error in the browser c ...

Ways to conceal and reveal content within div elements

I have a code snippet that is currently operational, but I am looking to enhance it by displaying one div at a time. If you would like to view the code, please visit my CodePen link below: https://codepen.io/danongu/pen/YzXvpoJ Due to the extensive amou ...

How can I customize the color of the check mark symbol in a bootstrap checkbox?

Here is my customized HTML code using Bootstrap: <div class="col-lg-4 col-12 text-center"> <div style="width: 80%; margin: auto"> <ul class="list-group"> {% for sl in my_list %} <li ...

Using CSS GRID for creating layouts with customized grid-template-areas that include empty cells and automatic placement

Struggling with implementing the css grid layout module to showcase a 12-column, 3-row grid. The initial row should only contain two elements, positioned on each side of the grid with empty cells in between using ten periods. Subsequent rows should autom ...

Is it possible to create two header columns for the same column within a Material UI table design?

In my Material UI table, I am looking to create a unique header setup. The last column's header will actually share the same space as the previous one. Picture it like this: there are 4 headers displayed at the top, but only 3 distinct columns undern ...

Having trouble accessing the files on MongoDB

I'm currently facing an issue while trying to access my documents stored in a MongoDB database. My approach involves making an AJAX GET request to my Node server, where the server is expected to locate the documents based on the value of the "breed" ...

What could be causing my ExpressJS server to not recognize POST requests?

const express = require('express'); const app = module.exports = express.createServer(); // Configuration app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', 'jade&apo ...

What is the best method for modifying an XML document without altering its associated XSL stylesheet?

I am working with XML data retrieved from a URL: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="/style.xsl"?> ....etc... To display the data in HTML format, I added the second line referencing the style.xsl file ...

Angularjs still facing the routing issue with the hashtag symbol '#' in the URL

I have recently made changes to my index.html file and updated $locationProvider in my app.js. After clicking on the button, I noticed that it correctly routes me to localhost:20498/register. However, when manually entering this URL, I still encounter a 4 ...

A collection of jQuery objects that consist of various DOM elements as their properties

Seeking a more concise and potentially more streamlined approach using jQuery. I have an object called lbl which represents a div. Inside this div, there is a span tag that contains the properties firstName and lastName of the lbl object. Here's how t ...

Include an additional feature to an already established design

I have been attempting to move an element to the left using the transform property. However, there is already a pre-existing style with transform, which appears as: transform: translate3d(tx, ty, tz) What I am aiming for is to incorporate another property ...

Is forming connections achievable through the acts of accepting and bypassing?

My entity is called UserEntity and it has relations described here for Following/Followers. Here's the image: enter image description here const user = await this.userRepository.findOne({ where: { id: anotherUserId || userId, // following: { ...