Reputation: 4361
I am learning Mean.js
stack, and try to build an app. I have installed Express, and it works. When I tried to configure my static file ( html, js, images, etc.), then things broke.
Here are my files:
server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + "public"));
app.listen(3000);
console.log('Server running on port 3000');
My html file is very simple :
<!DOCTYPE>
<html>
<head>
<title>Contact List App</title>
</head>
<body>
<h1>Contact List App</h1>
</body>
</html>
So when I start the server : node server.js
, and then I type http://localhost:3000/
in the browser, I get the "Cannot Get" error.
Where is the problem?
Upvotes: 3
Views: 8411
Reputation: 569
You need to make sure the route exists. Also, it is a better practice to use path for joining strings. Also, make sure the directory public
exists and the file index.html
is inside that folder.
var path = require('path');
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.render('index.html');
});
app.listen(3000);
console.log('Server running on port 3000');
Upvotes: 1
Reputation: 203231
__dirname
doesn't have a trailing slash, so you need to provide one yourself when building the static root:
app.use(express.static(__dirname + "/public"));
^ this needs to be there
Upvotes: 1