0

I have an existing coded frontend i.e views,javascripts,stylesheets (https://github.com/stdrunk/Taskr) and I intend to add this to the express framework so that i can link it to the db. I added the contents to the public folder. The javascripts in the javascript folder, css in stylesheets, and images in images folder. Then i changed the code of app.js according to this Render basic HTML view?

Now when run app.js and open the page in the browser i get a stripped version of my original page. No error comes in the console. This is my app.js

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');

var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
//app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);

app.use(express.static(__dirname + '/public'));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);


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



http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
Community
  • 1
  • 1
Ashish
  • 679
  • 7
  • 19
  • Please post the app.js file. Do you have the public directory setup in the config? app.use(express.static(path.join(__dirname, '/public'))); – BingeBoy Dec 28 '13 at 13:27

1 Answers1

0

You could put all those dirs under a 'public' dir, and then use:

app.use(express.static(__dirname + '/public'));

That way, express will always just send anything requested from those directories, and you won't need to worry about static files at all.

Although, I do recommend keeping something like Apache running on your server to serve static files. Images especially.

Chris
  • 1,611
  • 12
  • 11
  • I managed to fix all the errors that were showing in the console. It was a very silly mistake of "addresses" from my end. But still the page doesnt show up as expected. I did put all those dirs under public and added your line. But still it didnt work. – Ashish Dec 28 '13 at 13:59
  • What does your browsers Net output show? As in, what are the full paths your browser is requesting from the server? – Chris Dec 28 '13 at 14:02
  • For example, if your browser requests: www.site.com/js/file.js then this .js file should be in /public/js/file.js – Chris Dec 28 '13 at 14:03
  • See..what i have done is that i added one single index.html file in the views folder..now in that index.html i have linked all the js and css files that i have used.. there are 3 views in the page..the left one is totally missing.. – Ashish Dec 28 '13 at 14:09
  • How are you including the other views in your index.html file? I use EJS myself, but have the Jade API open right now. Check your paths, etc. – Chris Dec 28 '13 at 14:30
  • And btw, you only need one of the app.use(express.static(...)); calls. – Chris Dec 28 '13 at 14:40
  • Fixed the problem. It was just css indentation problem. Srry but i am really new to express and node. I am having a hard time understanding it. Can you tell me how to connect the elemets to database ? – Ashish Dec 28 '13 at 14:49