Saturday 8 April 2017

Loading html, css with jpg using node.js

I'm trying to create a web server that serves an html, css and jpeg file from a folder using node.js without express or any other frameworks.So far my it is working with a problem. The heading and first few sentences are not showing up. Here's what it looks like http://imgur.com/G3LgtVW

My folder structure looks like this:

folderName    
├── index.html    
├── style.css    
├── penguin.jpg    
test.js (which contains)



 var http = require('http'),
    fs = require('fs');

var handleRequest = function handleRequest(request, response){
        if (request.url==='/index.html' || request.url==='/') {
            response.writeHeader(200, {'Content-type':'text/html'});
            fs.readFile('./public/index.html', function (err, file) {
                if (err) throw err;
                response.end(file);
            });
        } else if (request.url.indexOf('jpg') != -1) {
            response.writeHeader(200, {'Content-type':'image/jpg'});
            fs.readFile('./public/apollo11.jpg', function (err, file) {
                if (err) throw err;
                response.end(file);
            });
        } else if (request.url.indexOf('css') != -1) {
            response.writeHeader(200, {'Content-type':'text/css'});
            fs.readFile('./public/style.css', function (err, file) {
                if (err) throw err;
                response.end(file);
            });
        }       
}
var server = http.createServer(handleRequest);

server.listen(1515, function(){
    console.log("Server listening on: http://localhost:%s", 1515);
});

index.html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="style.css"/>
    <title>Penguin</title>
</head>
<body>
    <main>
        <h1>Penguins</h1>
        <p>Penguins (order Sphenisciformes, family Spheniscidae) are a group of aquatic, flightless birds. They live almost exclusively in the Southern Hemisphere, with only one species, the Galapagos penguin, found north of the equator. Highly adapted for life in the water, penguins have countershaded dark and white plumage, and their wings have evolved into flippers. Most penguins feed on krill, fish, squid and other forms of sealife caught while swimming underwater. They spend about half of their lives on land and half in the oceans.

Although almost all penguin species are native to the Southern Hemisphere, they are not found only in cold climates, such as Antarctica. In fact, only a few species of penguin live so far south. Several species are found in the temperate zone, and one species, the Galápagos penguin, lives near the equator..The largest living species is the emperor penguin (Aptenodytes forsteri): on average adults are about 1.1 m (3 ft 7 in) tall and weigh 35 kg (77 lb) or more. The smallest penguin species is the little blue penguin (Eudyptula minor), also known as the fairy penguin, which stands around 40 cm (16 in) tall and weighs 1 kg (2.2 lb). Among extant penguins, larger penguins inhabit colder regions, while smaller penguins are generally found in temperate or even tropical climates (see also Bergmann's rule). Some prehistoric species attained enormous sizes, becoming as tall or as heavy as an adult human. These were not restricted to Antarctic regions; on the contrary, subantarctic regions harboured high diversity, and at least one giant penguin occurred in a region around 2,000 km south of the equator 35 mya, in a climate decidedly warmer than today.</p>
        <figure>
            <img src="penguin.jpg" alt="Penguin"/>
            <figcaption>Adélie penguin (Pygoscelis adeliae) feeding young. Like its relatives, a neatly bi-coloured species with a head marking. </figcaption>
        </figure>
    </main>
</body>
</html>

style.css

html, body {
    height: 100%;
}

body, h1 {
    margin: 10px;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
}

main {
    text-align: center;
    color: navy;
}

figcaption {
    font-size: 1.5em;
    font-family: cursive;
    font-size: 0.8em;
    color: #001125;
}



via user7836693

No comments:

Post a Comment