2011/11/23 dtang85 <dtan...@gmail.com>:
> I have a view in my Node app, but the external CSS file does not link
> properly. How do I allow the server I created to send that resource?
> Im very new to this so even a hardcoded static solution would be good.
>
> Here is the JS code that I run in Node:
>
> var http = require('http');
> var url = require('url');
> var fs = require('fs');
>
> var newPostFormHTML = fs.readFileSync('views/home.html');
>
> function renderNewPostForm(request, response) {
>        response.writeHead(200, {
>                'Content-type':'text/html; charset=utf-8'
>        });
>
>        response.end(newPostFormHTML);
> }
>
> function render404(request, response) {
>        response.writeHead(404);
>        response.end('404 File not found.');
> }
>
> var server = http.createServer(function(request, response) {
>        var newPostFormRegex = new RegExp('^/home/?$');
>        var pathname = url.parse(request.url).pathname;
>        if(newPostFormRegex.test(pathname)) {
>                renderNewPostForm(request, response);
>        }
>        else {
>                render404(request, response);
>        }
> });
>
> server.listen(8000);
>
> console.log('listening on http://127.0.0.1:8000');
>

var server = http.createServer(function(request, response) {
        var newPostFormRegex = new RegExp('^/home/?$'),
                stream,
                pathname = url.parse(request.url).pathname;
        if(newPostFormRegex.test(pathname)) {
                renderNewPostForm(request, response);
        }
        else if (pathname === '/style.css') {
                // Open a file stream to the file
                stream = fs.createReadStream('style.css');
                response.writeHead(200, {
                        'Content-type': 'text/css; charset=utf-8'
                });
                // On the stream end, end the response.
                stream.on('end', function () {
                  response.end();
                });
                // Pipe the file stream to the response.
                stream.pipe(response);
        }
        else {
                render404(request, response);
        }
});


I'll leave the rest of the logic up to you.

-- 
Poetro

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to