jeudi 21 juillet 2016

create website with Node.js

I am working with nodejs and now i am going to create static website with Node.js. It is very easy so please follow the steps after that i will post application of crud with bootstrap, nodejs and mysql.

        ## Create folder public within it and create three file with the name home.html, contact.html, about.html
        ## After that create a page web.js

        var http = require('http');
        var fs = require('fs');
        http.createServer( function(request,response) {
            var url = request.url;
            switch(url) {
            case '/':
            getStaticFileContent(response,'public/home.html','text/html');
            break;
            case '/about':
            getStaticFileContent(response,'public/about.html', 'text/html');
            break;
            case '/contact':
            getStaticFileContent(response,'public/contact.html','text/html');
            break;
            default:
            response.writeHead(404,{'Content-Type':'text/plain'});
            response.end('404-page not foud');
        }

        }).listen(4585);

        function getStaticFileContent(response, filepath, ContentType) {
            fs.readFile(filepath, function(error, data) {
                if(error) {
                        response.writeHead(500,{'Content-Type':'text/plain'});
                        response.end('500- internal server Error');
                } if(data) {
                    response.writeHead(200,{'ContentType':'text/html'});
                    response.end(data);
                }
            });
        }

Aucun commentaire:

Enregistrer un commentaire