javascript - node server.js - Cannot GET- Stack Overflow

admin2025-04-20  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?

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?

Share Improve this question asked Jan 4, 2017 at 9:45 James Marshall-OsborneJames Marshall-Osborne 1043 silver badges11 bronze badges 2
  • 1 are you sure to send __dirname + '/public' ? shouldn't it be __dirname + '/public/index.html' or something like that? – Adam 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 – James Marshall-Osborne Commented Jan 4, 2017 at 9:51
Add a ment  | 

3 Answers 3

Reset to default 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.

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).

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");
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745125394a286395.html

最新回复(0)