I
I
ince2018-12-11 12:33:05
JavaScript
ince, 2018-12-11 12:33:05

How to run a site on node.js?

Started the server on node express.js:

var express = require('express');
var app = express();
var path = require('path');

//
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/site/index.html'));
});

app.listen(80);

Everything seems to work, but JavaScript scripts on the page do not run and CSS styles are not applied.
Why does this happen and how to cure it?
Maybe you need to do it not through res.sendFile?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
shmatuan, 2018-12-11
@ince

Register where the styles are
https://expressjs.com/ru/starter/static-files.html

app.use(express.static(__dirname + '/dist'))
app.use(express.static(__dirname + '/node_modules'))

K
Kirill Kudryavtsev, 2018-12-11
@Deissh

The problem is that you are not distributing statics, think about how js / css will be loaded if they are not loaded from the server.

D
Dmitry Kuznetsov, 2018-12-11
@dima9595

If the index.html page itself loads (text is displayed, etc.), then you probably just specified the wrong path to the css and js files.

M
Magnus Keef, 2018-12-12
@SecurityYourFingers

Find the channel "Gosha Dudar" on YouTube. Watch
his video , everything is simple and clear there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question