static files in Express

119 views
Skip to first unread message

Vita Quasus

unread,
Nov 2, 2016, 3:57:49 PM11/2/16
to Express
I am trying to learn Node.js, I just installed express and am trying to build a HTML page, please see app1.js 

var express=require('express');
var app=express();
var port=process.env.PORT||3000;

app.use(express.static('public'));

I read the page at Express,  and believe this should allow me to access my public folder and get the style.css page later in my code. 

This next snippet of code is what I believe is causing me all my grief

app.get('/',function(req,res)
{
res.send("<html><head>"
+"<title> 9/3 </title>"
+"<link href='public/style.css' type='text/css' rel='stylesheet'/>"
+"</head><body><h1>Hello World!</h1><h2>"
+"Using Express to make my life easier</hr></body></html>");
});

The link for my css appears correct and when I look at the source code of the page it is correct. Yet no CSS is used. 
All my CSS is trying to do is to set    color:red:

 
+"<link href='public/style.css' type='text/css' rel='stylesheet'/>"


bodycolor: red; }

I attached both files, again the css is in my public folder which is in the folder my app1.js file is in. 

Why will the CSS not work? 
Where can I go to learn more about this? 

Thanks
app1.js
style.css

Christian Andersen

unread,
Nov 8, 2016, 1:04:07 AM11/8/16
to Express
I think the issue is that you're serving the html with the res.send(...) function but not serving the css at all since it is just in your local file system. Basically your link tag href is referencing a path that doesn't exist on the server. You'll probably want to create a separate html file and serve an entire directory. See this link for more information. (http://stackoverflow.com/questions/33500892/how-do-i-serve-multiple-files-in-node)
Reply all
Reply to author
Forward
0 new messages