Search code examples
htmlnode.jsexpressraspberry-piejs

nodejs with express and ejs different on ubuntu and rasbian


I want to build a small calendar with Express and EJS on Node.js. I started on my laptop (Ubuntu) and everything is running fine.
After that I cloned the project onto my Raspberry that will serve this small web server inside my home network. But it is throwing an error even the "data" inside the object it complains about is present.

index.ejs:

<!DOCTYPE HTML>
<html>
    <head>
        <title>Little Advent Calendar</title>
        <link rel="stylesheet" type="text/css" href="/main.css"  />
    </head>
    <body class="background">
        <div class="center">
            <section >
                <h2 class="title" align="center"> <%= day %>. Dezember</h2>
                <p class="btn-style" align="center">
                    <a href="/<%= day %>" type="button" class="a-btn">Öffnen</a>
                </p>
                
            </section>
        </div>
    </body>
</html>

server.js (running the web server):

...
app.use(express.static(path.join(__dirname, '/public/')));
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: false }));
app.use(methodOverride('_method'));

app.get('/', async(req, res) => {
    let date_ob = new Date();
    let day = date_ob.getDate();
    res.render('main/index', {"day": day});
    
})

app.listen(port);

On both machines the webpage looks the same. Including the date (day) is correctly displayed via the JSON object delivered from the server.js.
But on my Raspberry it is throwing the following error, it did not crash fully but it seems that something is wrong there:

ReferenceError: /home/pi/Desktop/advent-calendar/views/main/index.ejs:10
    8|      <div class="center">
    9|          <section >
 >> 10|                 <h2 class="title" align="center"> <%= day %>. Dezember</h2>
    11|                 <p class="btn-style" align="center">
    12|                     <a href="/<%= day %>" type="button" class="a-btn">Öffnen</a>
    13|                 </p>

day is not defined
    at eval ("/home/pi/Desktop/advent-calendar/views/main/index.ejs":12:26)
    at index (/home/pi/Desktop/advent-calendar/node_modules/ejs/lib/ejs.js:703:17)

Is there any difference in the JSON providing for Raspberry and e.g. Ubuntu within Node.js?

This is how the webpage looks. It is definetly printing the day comming in the day variable from the server.js

This is how the webpage looks. It is definetly printing the day comming in the day variable from the server.js


Solution

  • I found the Problem here. The main cause was that the browser was additional crawling the webserver for the favicon.ico.
    when this is not all the time a problem for me it was, because i also implemented a route to get the current data for the day by adding:

    app.get('/:day', async(req, res) => {
            var dayNum = req.params.day
            var day = today();
    
            // decide the type and render the specific page
            if (day.type == "number" || day.type == "Number") {
                res.render('main/number', { "day": day });
            } else {
                // default page if nothing matches (fallback)
                res.render('main/index');
            }
    })
    

    Inserting a check if the provided parameter in the url ist not the favicon.ico solved it for me. So the browser was calling this route even when im still on the index page.

    The error that day is not defined was raisded because i miss to insert the { day: day } for the index page in this route above. So technically because of the favicon.ico call my index page was rendered twice. The first time with the correct value and the second time without the value.