0

How can i add a .css file to my node.js(not express) code? Iam doing a simple beginners code. I tried the following but its not working:

    function start(response) {
    console.log("Request Handler start was called");
    var body = '<html>' + 
               '<head>' +             
               '<link href="style.css" rel="stylesheet" type="text/css">' +
           '</head>' +
           '<body >' +
           '<center><h1>Hello World</h1></center>' +
           '<marquee>Welcome!!!</marquee>'+
           '<br>'+
           '<form action="/upload" method="post">' +
           '<textarea name="text" rows="8" cols="40" ></textarea>' +
           '<input type="submit" value="submit text" />' +
           '</form>' +
           '</body>' +
           '</html>';
    response.writeHead(200,{'Content-Type': 'text/html'});
    response.write(body);
    response.end();
}

and my style.css goes thus:

body { margin-left : 10% ; margin-right : 10% ; margin-top: 10% ; margin-bottom : 10%; background : #D8BFD8; }
h1 { font-weight: bold;color : #8A084B;text-align: center;}
marquee { color:#305d7b; }
p {background: #acbeca; width: 700px; color: black;font-style: italic;}

2 Answers 2

2

This wont work as you have not defined the /style.css route in your node app.

You can approach this in two ways.

  1. Embed the css directly in your html file. Then the css will get applied.

  2. Write a new route for /style.css where you output the css file contents to the client and keep your html file as such.

Sign up to request clarification or add additional context in comments.

2 Comments

i have tried the first option and it works.but i wanted to know how i can write my css code in a separate file in my computer and use it in my node html code
i mean similar to how we use css in html.
1

I'm not sure how your code looks like, but you should have something like this:

var serverHTML = function(res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    var body = '<html>' + 
       '<head>' +             
       '<link href="/style.css" rel="stylesheet" type="text/css">' +
   '</head>' +
   '<body >' +
   '<center><h1>Hello World</h1></center>' +
   '<marquee>Welcome!!!</marquee>'+
   '<br>'+
   '<form action="/upload" method="post">' +
   '<textarea name="text" rows="8" cols="40" ></textarea>' +
   '<input type="submit" value="submit text" />' +
   '</form>' +
   '</body>' +
   '</html>';
    res.end(body + '\n');
}
var serveCSSData = function(res) {
    res.writeHead(200, {'Content-Type': 'text/css'});
    var css = '\
        body { margin-left : 10% ; margin-right : 10% ; margin-top: 10% ; margin-bottom : 10%; background : #D8BFD8; }\
        h1 { font-weight: bold;color : #8A084B;text-align: center;}\
        marquee { color:#305d7b; }\
        p {background: #acbeca; width: 700px; color: black;font-style: italic;}\
    ';
    res.end(css + '\n');
}
var http = require('http');
http.createServer(function (req, res) {
    switch(req.url) {
        case "/style.css": serveCSSData(res); break;
        default: serverHTML(res);
    }   
}).listen(1337, '127.0.0.1');
console.log('Server running at 127.0.0.1:1337/');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.