Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting express.js to show a public folder

I've recently built a quick one page app using express.js, and this is really my first js framework (actually, my first js project, so I'm really new to this). I've got a subscription to the new typography.com cloud fonts, and I'm having trouble locating the "fonts" folder that I place in the public folder. Is there a way I need to add the folder and specify a route?

Here's my app.js file:

/**
 * Module dependencies.
 */

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

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static('public'));

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

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

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

I haven't made any changes to the routes at all, so they are just default.

like image 579
Spentacular Avatar asked Dec 07 '22 06:12

Spentacular


1 Answers

Please use express.static setting:

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

And place your fonts folder in that public folder.

like image 140
moka Avatar answered Dec 22 '22 04:12

moka