2

I am using express to build a simple login app. I am using ejs as my template engine.the file structure of my application is

package.json
server.js
routes
   index.js
   users.js
views
   layouts.ejs
   login.ejs
   register.ejs
   welcome.ejs
public
   css
      bitnami.css

bitnami.css contains the css code I have downloaded from bootswatch

server.js has following code

const express = require('express');
const app = express();
const PORT = process.env.PORT;
const expressLayouts = require('express-ejs-layouts');
const path = require('path');
global.appRoot = path.resolve(__dirname);
console.log(path.join(global.appRoot,"public"));

//EJS
app.use(expressLayouts);
app.set('view engine','ejs');
//set static path
app.use(express.static(path.join(global.appRoot,"public")));
//Routes
app.use('/',require('./routes/index'));
app.use('/users',require('./routes/users'));

app.listen(PORT,()=>{
    console.log(`server running at ${PORT}`);
})

index.js has

const express = require('express');
router = express.Router();

router.get('/',(req,res)=>{
    res.render('welcome',{layout : 'layouts'});
})

users.js has

const express = require('express');
router = express.Router();
router.get('/login',(req,res)=>{
    res.render('login',{layout : 'layouts'});
})
router.get('/register',(req,res)=>{
    res.render('register',{layout : 'layouts'});
})

module.exports = router;

    module.exports = router;

layouts.ejs has following code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>BNI EVENT</title>
    <link rel="stylesheet" href="css/bitnami.css">
    <script src="https://kit.fontawesome.com/9c59f82571.js" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container">
        <%- body %>

    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>

</html>

If I load the css file from a CDN or if I use a static middleware for each router that has something after '/' ex('/users') the css file works but I want to know if there is a less redundant solution to this problem

If users.js has the following code the CSS file works properly

const express = require('express');
router = express.Router();
const path = require('path');
// setting the static middleware for this route 
router.use(express.static(path.join(global.appRoot,"public")));
router.get('/login',(req,res)=>{
    res.render('login',{layout : 'layouts'});
})
router.get('/register',(req,res)=>{
    res.render('register',{layout : 'layouts'});
})

module.exports = router;
Nisha Dave
  • 669
  • 1
  • 9
  • 25
  • I've had the same problem using express router in a js module and the accepted answer still works for me, i.e. : `const head = \`Title\`+ \`\`+ \`\`+ \`\`;` – Toni BCN Apr 25 '22 at 11:29

1 Answers1

3

This is a very basic problem

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>BNI EVENT</title>
    <link rel="stylesheet" href="css/bitnami.css">
    <script src="https://kit.fontawesome.com/9c59f82571.js" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container">
        <%- body %>

    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>

I was using css/bitnami.css in the layout file so whenever I went to some route let's say url.com/add then browser was searching for the file in add/css/bitnami.css. to achieve that browser always looks for the css folder in the main directory I had to use /css/bitnami.css

Correct code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>BNI EVENT</title>
    <!-- change is here -->
    <link rel="stylesheet" href="/css/bitnami.css">
    <script src="https://kit.fontawesome.com/9c59f82571.js" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container">
        <%- body %>

    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>

</html>
Nisha Dave
  • 669
  • 1
  • 9
  • 25