0

I am currently working on a project which is a webpage using angular to dynamically change DOM elements. Within the project is a public folder which contains all HTML, CSS, JavaScript and JSON objects. The project must be distributed so I am using node to run from localhost. This is my server.js code:

var express = require('express');
var app = express();
var path = require('path');

app.get('/', function(req,res){
   res.sendFile(path.join(__dirname + '/public')); 
});

app.listen(8080, function(){
    console.log((new Date()) + " Server is listening on port 8080");
});   

When I head to localhost:8080 it just says Cannot GET /. Where am I going wrong?

2
  • 1
    are you sure to send __dirname + '/public' ? shouldn't it be __dirname + '/public/index.html' or something like that? Commented Jan 4, 2017 at 9:49
  • That will only send the index.html file and not the associated files for the javascript or css. Im looking to serve the whole public folder so that these files will be rendered along with the HTML Commented Jan 4, 2017 at 9:51

3 Answers 3

3

The correct way to serve static files with express is as follows:

//Look for statics first
app.use(express.static(path.join(__dirname, 'public')));
//Return the index for any other GET request
app.get('/*', function (req, res) {
    res.sendFile('index.html', {root: path.join(__dirname, 'public')});
});

Edit: on a side note this may be worthwhile to mention that app.get should be the last route declared in node so if you want API endpoints exposed declare them above (before) the final app.get.

Sign up to request clarification or add additional context in comments.

6 Comments

That would then return index.html for URLs which really should be 404 error pages. That complicates all sorts of debugging because you get 200 OK messages displayed in dev tools when you put the wrong URL to (for example) an image or a JavaScript file.
He uses angularjs. This is required for html5mode to work correctly.
The URLs should be specified explicitly (and should return server rendered pages which Angular can progressively enhance, otherwise there is no point in using html5mode).
Followed Muli's code and it works. Page is now working 100%! Many thanks.
@JamesMarshall-Osborne — It works, but the app.get call has negative side effects and is unnecessary to solve the problem.
|
0

You forgot to point to the actual html file you want to display. If you have a index.html in your public directory, just point tot '/public/index.html' . That works (tested it here).

Comments

0

Followed user Muli's answer and all files are now being served correctly. Code here:

var express = require('express');
var app = express();
var path = require('path');

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

app.get('/', function(req,res){
   res.sendFile('index.html', {root: path.join(__dirname, './public')}) 
});

app.listen(8080, function(){
    console.log((new Date()) + " Server is listening on port 8080");
});

1 Comment

app.get('/', function(req,res){ ... }); is pointless. It does nothing that app.use(express.static(path.join(__dirname, 'public'))); doesn't already do. (Muli's code had a * in the path, which was harmful rather than pointless).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.