1

I had a problem: I need to connect the html to the static files on the server. I was sitting on many forums to find information, but it did not help, and the browser did not see the files.

My server.js code:

const https = require('https');
const routing = require('./routing');
const fs = require('fs');
var express = require('express');
var app = express();

var options = {
    key: fs.readFileSync('keys/server.key'),
    cert: fs.readFileSync('keys/server.crt')
};

app.use(express.static(__dirname + "/public"));

https.createServer(options, function (req, res) {
    var jsonString = '';
    res.setHeader('Content-Type', 'application/json');

    req.on('data', (data) => { 
        jsonString += data;
    });

    req.on('end', () => {
        routing.define(req, res, jsonString); 
    });
}).listen(3000, () => {
    console.log('Server on!');
    console.log('Port: ' + "3000");
});

And HTML code:

<script type="text/JavaScript" src="/main.styles_and_scripts/index.js"></script> 

<link rel="stylesheet" type="text/css" href="/main.styles_and_scripts/index.css">

The server does not see these files, what should I do?

File tree for server.js: var/www/html/nodejs/server.js,

File tree for html: var/www/html/nodejs/routing/main/index.html,

File tree for static files: var/www/html/nodejs/routing/public/main.styles_and_scripts/style.css (script.js).

tageerboy
  • 11
  • 2
  • https://expressjs.com/en/starter/static-files.html https://stackoverflow.com/questions/10434001/static-files-with-express-js – Adam H Sep 18 '18 at 16:24

0 Answers0