0

I have a ExpressJS server with ejs template engine runs a Blockly app and a threejs map in a web page. My problem is that, when I go to /applications/newApp, app.ejs works well but when I go to /applications/:slug (this is a request to load a saved application) the app.ejs crashes with error below:

Uncaught SyntaxError: Unexpected token '<' (at file.js:1:1)
Uncaught SyntaxError: Unexpected token '<' (at anotherFile.js:1:1)

Here is my ejs codes:

    <p hidden id="pageName">
      <% if(page_name=="savedApp" ) { %>
        savedApp
        <% } else { %>
          newApp
          <% } %>
    </p>

    <p hidden id="slug">
      <% if(page_name=="savedApp" ) { %>
        <%= slug %>
        <% } else { %>
          none
          <% } %>
    </p>

these both calles same file but one works well while another crashes. These are also how server responses;

exports.getNewApp = (req, res) => {
    res.render('app', {
        page_name: 'newApp',
    });
};
exports.loadApplication = async (req, res) => {
    try {
        res.render('a', {
            page_name: 'savedApp',
            slug: req.params.slug,
        });
    } catch (err) {
        console.log(err);
        res.status(500);
    }
};

WHAT I TRIED:

  • I checked the opened <% %> tags, there is no error in here
  • I am using public files like that:
app.use(express.static(path.join(__dirname + 'public')));
app.use('/applications', express.static(path.join(__dirname + 'public/blocklyApplication')));

And I also checked following questions:

Express.js, Node.js, EJS - Uncaught SyntaxError: Unexpected token <

SyntaxError: Unexpected token ; while compiling ejs

SyntaxError: Unexpected token ,while compiling ejs

I don't get that, they are both responses same page with common url, one works well other doesnt. What should I check?

1 Answers1

0

I don't know why, after trying some changes in code, adding

app.use(
  '/applications/:slug',
  express.static(path.join(__dirname + '/public/blocklyApplication'))
);

in my code makes everything work.

EDIT: and also in here Express-js can't GET my static files, why? there is a detailed answer about routed public.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31