Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  124] [ 7]  / answers: 1 / hits: 29077  / 11 Years ago, mon, december 2, 2013, 12:00:00

I'm currently experiencing some minor problems with serving static files through expressJs.



My directory structure is as following:




  • public


    • css

    • lib


  • src


    • views

    • home


      • index.html


    • server.js




In my index.html file i have prefixed all my assets with a leading slash.



My static setup is as following:
app.use(express.static(path.resolve(__dirname + '../' + 'public')));



But for some reason my static files are not getting served.



I was thinking that this is a crossdomain call or something...
I'm currently using cloud9 IDE, might this have to do with it somehow?


More From » node.js

 Answers
51

You should use path.join instead of manually concatening path components. It uses path.normalize, which resolves . and .., handles multiple or trailing slashes, and uses the appropriate file separator for your platform (see: path.sep).



For example,



var path = require('path');

var express = require('express');

var app = express();

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

[#73948] Saturday, November 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
minab

Total Points: 701
Total Questions: 104
Total Answers: 91

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;