1

This is html page.I'm calling this page from node.js server i.e, app.js.Here I'm unable to load socket.io.js,bootstrap.js,bootstrap.css pages.

My index.html:

<html>
<script type="text/javascript" src="/javascripts/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type ="text/javascript" src="/javascripts/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="/stylesheets/bootstrap.css" />


<div id="progressbar">
</div>

<script>
  var socket = io('http://localhost:8085');
  socket.on('connect', function(){});
  socket.on('message', function(data){
    $('#progressbar').progressbar({
    maximum: 100,
    step: JSON.parse(data).percent
    });
  });
  socket.on('disconnect', function(){});
</script>
</html>

My app.js code :

var app = express ();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(8085, function(){
    console.log('listening on *:8085');
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

app.get('/', function (req, res) {
 res.sendfile(__dirname + '/index.html');
//res.send('Hello World!');

});

In firebug,I'm facing this enter image description here

13
  • 2
    Can you post your app.js, the part where you include express.static? Commented Nov 17, 2015 at 9:53
  • Maybe also the folder structure where your app is running from Commented Nov 17, 2015 at 9:54
  • please check the app.js code Commented Nov 17, 2015 at 9:57
  • Make sure the path is correct. Can you see the file from localhost:8085/javascripts/socket.io.js ? Commented Nov 17, 2015 at 10:07
  • yes,I'm able to see the file Commented Nov 17, 2015 at 10:08

3 Answers 3

2

You have set up incorrect paths for static files.

In your case, you'll need to use this:

app.use('/javascripts', express.static(__dirname + '/javascripts'));
app.use('/stylesheets', express.static(__dirname + '/stylesheets'));

Now, whatever files are in javascripts and stylesheets folders will load as static files.

Your current method of using static files:

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

This will load everything that's beneath public folder as static files, so you would need to move the javascripts and stylesheets folders there if you want that to work.

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

5 Comments

Which of the options did you try?
I tried by changing to the above said code .It did not worked for me.Actually at this path,I'm getting the file localhost:8085/javascripts/socket.js
so in index.html,I changed like this,<script src="localhost:8085/javascripts/socket.io.js"></script> <script src="code.jquery.com/jquery-1.10.2.js"></script> <script src="localhost:8085/javascripts/bootstrap.js"></script> <link rel="stylesheet" type="text/css" href="localhost:8085/stylesheets/bootstrap.css" />
Create a very simple project example with this problem and put it on GitHub, I'll check it out.
Sorry, host it somewhere so I'm hopefully not the only one looking.
0

change your view engine to html and modify the code like below :

app.set('views', path.join(__dirname, 'public'));
app.set('view engine', 'html');

and change the sendfile method :

res.sendFile(path.join(__dirname, '../YOUR_FOLDER_NAME/public', 'index.html'));

EDITED::

change the index.html file:

<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<link rel="stylesheet" type="text/css" href="stylesheets/test.css" />

1 Comment

check the edited answer. you don't need to put the socket.io.js in javascript folder manually.just add the script and remove '/' before stylesheets and javascripts in script and link file .
0

This is the app.js. (based on @Andrius' solution) I was able to make it running on http://localhost:8085. index.html stays the same.

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

var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(8085, function(){
    console.log('listening on *:8085');
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
//app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

//test
app.use('/javascripts', express.static(__dirname + '/javascripts'));
app.use('/stylesheets', express.static(__dirname + '/stylesheets'));

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

//app.use('/', routes);
//app.use('/users', users);

app.get('/', function (req, res) {
 res.sendfile(__dirname + '/index.html');
//res.send('Hello World!');

});

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.