How to serve static files?


#1

Super new to web development, so please forgive the stupidity of this question.

I need to serve the contents of a directory called ‘public’ for a site I’m building. Standard stuff in it, just CSS right now actually.

I’m using node and express. I have the ‘serve-static’ and ‘path’ modules (which are necessary for this…?), and it seems like I’m using them correctly. But the styles aren’t being applied. At this juncture I don’t know if it’s because the directory isn’t being served or (more embarrassingly) because I have the file path wrong.

This is how I’m attempting to serve the folder in my app.js:

var serveStatic = require(‘serve-static’);
var path = require(‘path’);
app.use(serveStatic(path.join(__dirname, ‘public’)));

This is how I’m attempting to include it in a header ejs file:

< link rel=“stylesheet” type="/public/styles/landing.css" >

Thanks in advance. I’m loving C9 so far.


#2

The default config for apache will serve up any files under ~/workspace. This can be adjusted by changing the directory paths in /etc/apache2/sites-enabled/001-cloud9.conf.

You can start the server with services apache2 start

Simples. :slight_smile:


#3

I probably should’ve explained that I’m using MongoDB with Mongoose. Thank you though.


#4

Forgive me, I didn’t read the whole post. just saw that you was wanting to host static files.

I noticed that you’re trying to host a “public” folder. I also notice that you included this folder in the stylesheet path. I just wanna mention that for whatever folder you pick as the root of the web server, it is at that point you need to reference the files. Remove the /public bit from the header file and you should find that the styles work.

Cheers.


#5

Hey, thanks for taking the time to respond. Sorry it took me so long to answer - I was away for work. I’ll try what you suggested today.