Node.js. Why can't a page load css and javascript files?

I created a node.js. server When I entered localhost using port 3000, it only displayed text without css or javascript. I tried several solutions that worked on others. but they did not work for me.

Node JS continues to receive Failed to load resource error message

static files using express.js

Unable to get CSS file

my file order is similar to this

server.js
index.html
 public
  css
   style.css

This is server code

var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');});
app.use(express.static(__dirname + 'public'));
app.listen(3000);

These are mistakes

GET http://localhost:3000/ [HTTP/1.1 304 Not Modified 11ms]
GET http://localhost:3000/public/css/demo.css [HTTP/1.1 404 Not Found 10ms]
GET http://localhost:3000/public/css/themes/popclip.css [HTTP/1.1 404 Not Found 10ms]
GET http://localhost:3000/public/css/reveal.min.css [HTTP/1.1 404 Not Found 19ms]
GET http://localhost:3000/public/css/theme/default.css [HTTP/1.1 404 Not Found 12ms]
GET http://localhost:3000/public/css/jquery.dropdown.css [HTTP/1.1 404 Not Found 11ms]
GET http://localhost:3000/node_modules/socket.io/node_modules/socket.io-client/socket.io.js [HTTP/1.1 404 Not Found 10ms]
GET http://localhost:3000/public/js/jquery.min.js [HTTP/1.1 404 Not Found 29ms]
GET http://localhost:3000/public/js/jquery.popline.js [HTTP/1.1 404 Not Found 28ms]
GET http://localhost:3000/public/js/plugins/jquery.popline.link.js [HTTP/1.1 404 Not Found 28ms]
+4
source share
3 answers

It:

__dirname + 'public'

Must be:

__dirname + '/public'

, html/css , __dirname + '/public' . , __dirname + '/public/public', html/css.

+8

.

index.html. , css js, "public" src.

<script src="public/js/jquery.min.js"></script>
<script src="public/js/jquery.popline.js"></script>

"" ,

<script src="js/jquery.min.js"></script>
<script src="js/jquery.popline.js"></script>

'/' __dirname + 'public' __dirname + '/public', , mscdex.

+8

, , - .

, , :

app.use('/css', express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public'));
app.use(express.static(path.join(__dirname, '../public')));

'project', "" , "views" server.js. Node, Express Handlebars . 'public' css, style.css.

server.js

var express = require('express');
var app = express();
app.use(express.static('public'));

main.handlebars 'views' > 'layouts'

<link rel="stylesheet" href="/css/style.css">

, , "404" .

0

All Articles