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

I am currently working on a project which is a webpage using angular to dynamically change DOM elements

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");
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745125392a4612661.html

相关推荐

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

    I am currently working on a project which is a webpage using angular to dynamically change DOM elements

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信