Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot GET / Express ERROR

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?

enter image description here

like image 308
Pracede Avatar asked Apr 20 '26 01:04

Pracede


1 Answers

__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
like image 199
robertklep Avatar answered Apr 21 '26 15:04

robertklep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!