How to reference css and script files in node.js application?

I read this Node.js link - external JS and CSS files (just using node.js cannot be expressed) and comprehend a few, but still don’t know where to connect them, in my case. Consider the application "Hello World" node.js, 2 lines, a link and a script in the chapter section will not work. I think because they are not yet a web link. So how do I turn them on? If I really like the link, will they be outside the chapter?

var http = require('http');
var html = 
    '<html>'+
        '<head>'+
            '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'+
            '<link rel="stylesheet" type="text/css" href="./mystyle.css">'+
            '<script src="./myscript.js"></script>'+
        '</head>'+
        '<body>'+
            '<p>Hello World!</p>'+
        '</body>'+
    '</html>';

http.createServer(function(request, response) {
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.write(html);
    response.end();
}).listen(80);
+4
source share
3 answers

, ! , node.js . . .

var http = require('http');
var fs = require('fs');
var i = 0;

var html = 
    '<html>'+
        '<head>'+
            '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'+
            '<link rel="stylesheet" type="text/css" href="./mystyle.css">'+
            '<script src="./myscript.js"></script>'+
        '</head>'+
        '<body>'+
            '<p>Hello World!</p>'+
        '</body>'+
    '</html>';

http.createServer(function(request, response) {
    i++;
    console.log('Request #'+i+': '+request.url);
    if (request.url.indexOf('.js') != -1) {
        fs.readFile(__dirname + '/misc/myscript.js', function (err, data) {
            if (err) console.log(err);
            else {
                console.log('/misc/myscript.js: fs.readFile is successful');
                response.setHeader("Content-Length", data.length);
                response.setHeader("Content-Type", 'text/javascript');
                response.statusCode = 200;
                response.end(data);
            }
        });
    }
    else if (request.url.indexOf('.css') != -1) {
        fs.readFile(__dirname + '/misc/mystyle.css', function (err, data) {
            if (err) console.log(err);
            else {
                console.log('/misc/mystyle.css: fs.readFile is successful');
                response.setHeader("Content-Length", data.length);
                response.setHeader("Content-Type", 'text/css');
                response.statusCode = 200;
                response.end(data);
            }
        });
    }
    else {
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.write(html);
        response.end();
    }
}).listen(80);
0

response.writeHead, request.writeHead.

, js css.

+1

, . - .

, , , - connect static .

- :

var http = require('http');
var static = require('static')('/');

http.createServer(static).listen(80);

-, /.

0

Source: https://habr.com/ru/post/1535121/


All Articles