0

I am developing a express.js app. I am developing admin and users two routes. But in admin section I created sub directories like post, media, etc. I want to use static files like style sheet for post, media, etc.

/admin/posts/js/lib/jquery/jquery.min.js 404 92.978 ms - 19288

where this file locate in /admin/js/lib/jquery/jquery.min.js

I put all stylesheet and javascripts file in public/admin/. and I want use this stylesheet for all sub directories like posts, media, feedback, etc

    app.use(express.static(path.join(__dirname, 'public')));

1 Answers1

0

You can combine static files with dynamically generated content using Express. For example you can default to serving static content, then serve dynamic content for some routes. e.g.

index.js

const express = require('express');
const app = express();

app.use(express.static('./'));
app.get('/info', function(req, res) {
    res.json({ status: 'OK', timeStamp: new Date() });
});

app.listen(8080);

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Static Root</title>
        <link rel="stylesheet" type="text/css" href="/stylesheets/test.css">
    </head>
    <body>
        <h4 class="center">I am static content!</h4>
    </body>
</html>

You'd serve this initially from one folder with index.js and index.html. Any route other than /info will look for static files, e.g. / or /index.html will pull index.html.

You can easily add stylesheets for example, in a ./stylesheets folder

e.g.

/stylesheets/test.css

.center {
    text-align: center;
    color: green;
}

This is then referenced by index.html

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40