Tanvi Shah
Tanvi Shah

Reputation: 507

node express not getting bootstrap.js file

I have a directory structure:

projectName
    | -.sencha/
    | - app/
        | - view
        | - controller
        | - store
    | - saas
    | - server/
        | -server.js
    | - index.html
    | - bootstrap.js
    | - app.js

I would like to start my app and serve index.html with node. So in server/server.js I have:

app.get('/', function(req, res) {
  res.sendFile('index.html', { root: path.join(__dirname, '../') });
})

And this is working fine but after this my localhost throwing an error bootstrap.js is not found.

index.html

    <!DOCTYPE HTML>
    <html manifest="">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta charset="UTF-8">

        <title>serverApp</title>

        <!-- The line below must be kept intact for Sencha Cmd to build your application -->
        <script id="microloader" type="text/javascript" src="bootstrap.js"></script>

    </head>
    <body></body>
    </html>

So can you tell me how to solve this? or else Can you provide me a good link of example which is developed in extjs,express,nodejs,mongodb.

Upvotes: 0

Views: 89

Answers (2)

Adam
Adam

Reputation: 5253

You should rewrite your file structure a bit to serve static files:

Add this line to your server.js to serve static files from your public folder:

app.use('/', express.static('public'));

And modify your file structure to this:

projectName
    | -.sencha/
    | - app/
        | - view
        | - controller
        | - store
    | - saas
    | - server/
        | -server.js
    | - public/
        | - index.html
        | - bootstrap.js
        | - app.js

more info: http://expressjs.com/en/starter/static-files.html

Upvotes: 1

Dave Amit
Dave Amit

Reputation: 2309

I 100% agree with @Adam on this one! Additionally, you should resolve your public folders to absolute paths, its not a must but guidelines so as we don't get punches ... ;)

You should restructure your app as @Adam suggested, plus

Do the following, it will resolve your issue with good guidelines.

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var app = express();


 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());



//This will enable you to access it form '/'
app.use('/', express.static(__dirname + '/public', {index: "index.html"}));

// Rest of the stuff

Then if you will visit your URL that you set and port, you'll be able to access.

Using express.static is recommended way of serving static content.

Hope it helps!

Upvotes: 1

Related Questions