0

I'm new to node js. I'm trying to create a node js app and configured the app.js as follows.

var express = require ('express');

var app = express();

app.set('view engine', 'ejs');
app.use(express.static('resources'));

app.get('/', function(req, res){
    res.render('index');
});

app.listen(3000);

I'm using several partial views and inject all the partials to main.ejs and finally inject main.ejs into index.ejs

To apply bootstrap and jquery to all the views, I'm calling them in the index.ejs file.

Index.ejs:

<!DOCTYPE html>
<html>
<head>
    <title>New page</title>
    <link type="text/css" href="/resources/bootstrap.min.css">
    <script type="text/javascript" src="/resources/bootstrap.min.js"></script>
    <script type="text/javascript" src="/resources/jquery-3.3.1.slim.js"></script>
</head>
<body>
    <% include main.ejs %>
</body>
</html>

main.ejs

<!DOCTYPE html>
<html>
<head>
    <title>New page</title>
</head>
<body>
    <% include partials/partial_page.ejs %>
</body>
</html>

partial_page.ejs

<!DOCTYPE html>
<html>
<head>
    <title>New page partial</title>
</head>
<body>
    New Page 2
    <button class="btn btn-primary">AAAA</button>
</body>
</html>

All the static files are stored in resources folder.

These bootstraps and jquery is not working and I can't get any result from them. Console has 404 error which is Bootstraps and jquery cannot be found. What is the wrong here?

3 Answers 3

1

Try removing the <!DOCTYPE>, html, head, and body tags from all except index.ejs. You don't need them since the partials are being rendered inside index.ejs, and that's where your route is pointing.

Sign up to request clarification or add additional context in comments.

3 Comments

are you getting any errors in the console when you load your page?
console has 404 error. Bootstraps and jquery cannot be found
Ok then its most likely an issue with the paths you provided in your link and script tags. can you demonstrate your project folder layout in the question?
1

you can use app.use(express.static(__dirname + '/resources'));

change <script type="text/javascript" src="/resources/bootstrap.min.js"></script> to <script type="text/javascript" src="bootstrap.min.js"></script>

8 Comments

Tried it. But still the problem is there
I updated my question. The error is 404 error. Bootstraps and jquery cannot be found
change <script type="text/javascript" src="/resources/bootstrap.min.js"></script> to <script type="text/javascript" src="bootstrap.min.js"></script> and use app.use(express.static(__dirname + '/resources'));
Tried and still it can't find these files
I also tried with app.use('/resources', express.static('resources')); and doesn't working
|
0
var express = require ('express');

var app = express();


app.set('view engine', 'ejs');
app.use(express.static(__dirname,'resources'));
app.engine('html',require('ejs').renderFile);

app.get('/', function(req, res){
    res.render('index');
});

app.listen(3000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.