Reputation: 191
I have learning Node JS and try to open the Serving Static Files using the below function on node js express. But I got the Cannot GET error check the screenshot
I have a proper folder structure.
I refer to the below article https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm
app.use(express.static('public'));
Here is the full code
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/', function (req, res) {
res.send('Hello World');
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log(host, port)
})
Folder structure
node_modules
server.js
public/
public/images
public/images/Minions_characters.png
Upvotes: 0
Views: 60
Reputation: 944294
app.use(express.static('public'));
You didn't specify a path as the first argument to use
.
You've mounted the contents of the public
directory at /
Therefore the correct URL does not start with /public
Either:
/public
from the URL/public
to the mount point: app.use('/public/, express.static('public'));
Upvotes: 1