your onrequest should not write out all your html. instead it should just
deliver your static files. you could do it yourself, but better is you just
use a lib, that does it for you. there are several libs out there you can
use this example: https://github.com/isaacs/st-example or this
one http://www.senchalabs.org/connect/ or some specific skeleton
here https://github.com/inadarei/nodebootstrap
this can help you to start and not get lost in details first.
but if you're interested, very bare example to start is this one:
var http = require('http');
var url = require('url');
var fs = require('fs');
function onRequest(req, res) {
console.log('on request');
var pathname = url.parse(req.url).pathname;
var filename = __dirname + pathname
if(pathname === '/') filename = __dirname + '/index.html'
console.log(pathname + ' -> ' +filename)
if(fs.statSync(filename).isFile())
fs.createReadStream(filename).pipe(res);
else{
res.writeHead(404,'File Not Found');
res.end();
}
}
http.createServer(onRequest).listen(3000);
console.log('Server has started!')
this assumes following dir structure
somedir
-- server.js
-- index.html (contains your html code, you're building manually first,
btw. it was invalid)
-- css
---- bootstrap.min.css
-- js
---- bootstrap.min.js
and so on
so paths like http://myhost:3000/js/myScript.js would be served,
myhost:3000 just delivers index.html
happy learning
Am Mittwoch, 13. März 2013 11:15:19 UTC+1 schrieb Timo Schmidt:
>
> Hi,
>
> i am quite new to node.js. I am trying to use bootstrap to node.js and
> tried the classic way. I simply wrote the "getting started code" of
> bootstrap to node.js just to try it. But when I'm trying to use simple
> rows/spans I get no good output. When I check it with firebug, I can see,
> that there would be no rule in the bootstrap.css (but there are).
>
> Node.js Code:
> function onRequest(req, res) {
> var postData = "";
> var pathname = url.parse(req.url).pathname;
> console.log("Request recieved");
>
> res.writeHead(200, {"Content-Type": "text/html"});
> res.write("<head>");
> res.write("<title>Bootstrap 101 Template</title>");
> res.write("<meta name='viewport' content='width=device-width,
> initital-scale=1.0'>");
> res.write("<link href='css/bootstrap.min.css' rel='stylesheet'
> media='screen'>");
> res.write("</head>");
>
> res.write("<body>");
> res.write("<div class='container'>");
> res.write("<div class='row show-grid'>");
> res.write("<div class='span4'>4</div>");
> res.write("<div class='span8'>8</div>");
> res.write("</div>");
> res.write("</div>");
> res.write("<script src='jquery/jquery'></script>");
> res.write("<script src='js/bootstrap.min.js'></script>");
> res.write("</body>");
> res.end();
>
> }
>
> http.createServer(onRequest).listen(8888);
> console.log('Server has started!');
>
>
> HTML Code:
> <html debug="true">
> <head>
> <title>Bootstrap 101 Template</title>
> <meta name="viewport" content="width=device-width, initital-scale=1.0"/>
> <link href="css/bootstrap.min.css" rel="stylesheet" media="screen"/>
> <style type="text/css"/>
> </head>
> <body>
> <div class="container">
> <script src="jquery/jquery"/>
> <script src="js/bootstrap.min.js"/>
> </body>
> <script src="
> chrome-extension://bmagokdooijbeehmkpknfglimnifench/googleChrome.js"/>
> </html>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.