FAQ
Hey guys,
I'm doing a app 100% MVC with locomotive, i have one layout file that
load another files just to put the content, it's a small app, but very good
app... So, i want to know, the best way to load a html file, i google it
and found this solution:

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


fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);});


What do you guys think??

--
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
nodejs+[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Search Discussions

  • Mark Hahn at Dec 26, 2012 at 4:14 am
    That would work if you want to send out that one file no matter what
    request comes in. I doubt that is what you want to do.
    On Tue, Dec 25, 2012 at 7:40 AM, Rodrigo Fonseca wrote:
    Hey guys,
    I'm doing a app 100% MVC with locomotive, i have one layout file that load
    another files just to put the content, it's a small app, but very good
    app... So, i want to know, the best way to load a html file, i google it and
    found this solution:

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


    fs.readFile('./index.html', function (err, html) {
    if (err) {
    throw err;
    }
    http.createServer(function(request, response) {
    response.writeHeader(200, {"Content-Type": "text/html"});
    response.write(html);
    response.end();
    }).listen(8000);
    });


    What do you guys think??

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Pedro Teixeira at Dec 26, 2012 at 10:14 am
    If it's done during app initialization you should load it synchronously.

    --
    Pedro

    On Tuesday, 25 de December de 2012 at 15:40, Rodrigo Fonseca wrote:

    Hey guys,
    I'm doing a app 100% MVC with locomotive, i have one layout file that load another files just to put the content, it's a small app, but very good app... So, i want to know, the best way to load a html file, i google it and found this solution:

    var http = require('http'), fs = require('fs'); fs.readFile('./index.html', function (err, html) { if (err) { throw err; } http.createServer(function(request, response) { response.writeHeader(200, {"Content-Type": "text/html"}); response.write(html); response.end(); }).listen(8000); });

    What do you guys think??

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rodrigo Fonseca at Dec 26, 2012 at 4:04 pm
    Pedro,
    Yes, it's done during app initialization, how can i do this?? I´m
    begginer with node, can you explain more details about it?

    Mark,
    Ii want to do what Pedro said.

    cheers.


    >

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • José F. Romaniello at Dec 26, 2012 at 4:08 pm
    You can use readFileSync<http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_encoding>

    var http = require('http'),
    fs = require('fs');
    var html = fs.readFileSync('./index.html');

    http.createServer(function(request, response) {
    response.writeHeader(200, {"Content-Type": "text/html"});
    response.write(html);
    response.end();
    }).listen(8000);



    2012/12/26 Rodrigo Fonseca <[email protected]>
    Pedro,
    Yes, it's done during app initialization, how can i do this?? I´m
    begginer with node, can you explain more details about it?

    Mark,
    Ii want to do what Pedro said.

    cheers.

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rodrigo Fonseca at Dec 26, 2012 at 4:44 pm
    Thanks José!


    Em quarta-feira, 26 de dezembro de 2012 13h08min16s UTC-3, José F.
    Romaniello escreveu:
    You can use readFileSync<http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_encoding>

    var http = require('http'),
    fs = require('fs');
    var html = fs.readFileSync('./index.html');

    http.createServer(function(request, response) {
    response.writeHeader(200, {"Content-Type": "text/html"});
    response.write(html);
    response.end();
    }).listen(8000);



    2012/12/26 Rodrigo Fonseca <[email protected] <javascript:>>
    Pedro,
    Yes, it's done during app initialization, how can i do this?? I´m
    begginer with node, can you explain more details about it?

    Mark,
    Ii want to do what Pedro said.

    cheers.

    --
    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]<javascript:>
    To unsubscribe from this group, send email to
    [email protected] <javascript:>
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rodrigo Fonseca at Dec 27, 2012 at 3:31 am
    I've to create a server just to read a file??? I'm getting trouble with
    this, because always when i call a URL i create a server, because i put
    this inside of s function:

    http.createServer(function(request, response) { response.writeHeader(200, {
    "Content-Type": "text/html"}); response.write(html); response.end();
    }).listen(8000);

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Cooper at Dec 27, 2012 at 3:57 am

    On Wed, Dec 26, 2012 at 7:31 PM, Rodrigo Fonseca wrote:

    I've to create a server just to read a file???
    No, of course not. But your original code indicated that you want to read a
    file and then serve it. That's what Jose's code does - it reads the file
    (in one line of code), and then creates a server that serves the contents
    of that file each time a new request is made.

    I'm getting trouble with this, because always when i call a URL i create a
    server, because i put this inside of s function:
    No, the server is created once, and the (anonymous) function that was
    passed to createServer is called each time a request is handled. That
    function sends back the contents of the file that was read earlier.

    It might be a good idea for you to work through one or two tutorials on
    Node, so that you get a clearer picture of how it all works. You'll find
    pointers to several of those in the archives of this list. Those at
    http://www.nodetuts.com/ and http://www.nodebeginner.org/ seem to be fairly
    frequently referenced here.

    --
    Martin Cooper


    http.createServer(function(request, response) { response.writeHeader(200,
    {"Content-Type": "text/html"}); response.write(html); response.end();
    }).listen(8000);

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rodrigo Fonseca at Dec 27, 2012 at 4:06 pm
    Thanks Martin,
    I will read the tutorials.

    cheers.

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rodrigo Fonseca at Dec 27, 2012 at 8:21 pm
    No, the server is* created once*, and the (anonymous) function that was
    passed to createServer is called each time a request is handled. That
    function sends back the contents of the file that was read earlier.

    Ok, it´s created once, but, if there is another server that was created? I
    have to put the same port? Another thing interesting, when i put the same
    port or any other port in listen() is that when i reload in the first time
    my view is rendered normally, on the second time my view lose the css and
    on the third time i lost the contact with the server, but when i don´t put
    anything in listen, the view render normally, why? I'm reading these
    tutorial, understanding a bit the node, but this is really curious for me...

    It might be a good idea for you to work through one or two tutorials on
    Node, so that you get a clearer picture of how it all works. You'll find
    pointers to several of those in the archives of this list. Those at
    http://www.nodetuts.com/ and http://www.nodebeginner.org/ seem to be fairly
    frequently referenced here.
    I started with nodebeginner, i'm really enjoying, thanks again.




    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Cooper at Dec 28, 2012 at 1:12 am

    On Thu, Dec 27, 2012 at 12:21 PM, Rodrigo Fonseca wrote:

    No, the server is* created once*, and the (anonymous) function that was
    passed to createServer is called each time a request is handled. That
    function sends back the contents of the file that was read earlier.

    Ok, it´s created once, but, if there is another server that was created?
    I have to put the same port? Another thing interesting, when i put the same
    port or any other port in listen() is that when i reload in the first
    time my view is rendered normally, on the second time my view lose the
    css and on the third time i lost the contact with the server, but when i
    don´t put anything in listen, the view render normally, why? I'm reading
    these tutorial, understanding a bit the node, but this is really curious
    for me...
    You haven't mentioned CSS before, and you haven't shown us the HTML
    content, so it's a little hard to know what's going on. However, I should
    point out that you are responding to *every* request to your server with
    the contents of the HTML file. So if your CSS URL is also pointing to the
    same server, it's going to retrieve HTML content and not CSS content. That
    would not be good! You would need to look at the request, in your server,
    to determine what you should be sending back. Again, the tutorials should
    help you understand this better.

    --
    Martin Cooper


    It might be a good idea for you to work through one or two tutorials on
    Node, so that you get a clearer picture of how it all works. You'll find
    pointers to several of those in the archives of this list. Those at
    http://www.nodetuts.com/ and http://www.nodebeginner.org/ seem to be
    fairly frequently referenced here.
    I started with nodebeginner, i'm really enjoying, thanks again.




    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rodrigo Fonseca at Dec 28, 2012 at 2:31 pm
    *You haven't mentioned CSS before, and you haven't shown us the HTML
    content, so it's a little hard to know what's going on. However, I should
    point out that you are responding to *every* request to your server with
    the contents of the HTML file. So if your CSS URL is also pointing to the
    same server, it's going to retrieve HTML content and not CSS content. That
    would not be good! You would need to look at the request, in your server,
    to determine what you should be sending back. Again, the tutorials should
    help you understand this better.*
    *
    *
    * *Sorry, i'm doing a mcv app in node, i have a layout, so all i do is to
    put the content in this layout, so, if the url is "test", this call
    "test_controller", that render "layou/layout" and call "layout_controller"
    where is send to a function "layout" the object from "test_controller", see:

    LayoutController.layout = function(obj)
    {
    var http = require('http'),
    fs = require('fs');
    var html = fs.readFileSync('./app/views/'+ obj.__action+'/'+obj.fileName);
    http.createServer(function(request, response) {
    response.writeHeader(200, {"Content-Type": "text/html"});
    response.write(html);
    response.end();
    }).listen();
    obj.content = html;
    obj.render('layout/layout');
    }

    As can be seen, this function read in on single place every different obj
    that comes here, but, as you can see, the layout is all the page with css
    and javascript, it's always static, only the content changes. So, as you
    can see again, the listen obj is empty, and it's works perfectly, but when
    i put some port happens what i said before.

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Cooper at Dec 28, 2012 at 5:18 pm

    On Fri, Dec 28, 2012 at 6:31 AM, Rodrigo Fonseca wrote:

    *You haven't mentioned CSS before, and you haven't shown us the HTML
    content, so it's a little hard to know what's going on. However, I should
    point out that you are responding to *every* request to your server with
    the contents of the HTML file. So if your CSS URL is also pointing to the
    same server, it's going to retrieve HTML content and not CSS content. That
    would not be good! You would need to look at the request, in your server,
    to determine what you should be sending back. Again, the tutorials should
    help you understand this better.*
    *
    *
    * *Sorry, i'm doing a mcv app in node, i have a layout, so all i do is to
    put the content in this layout, so, if the url is "test", this call
    "test_controller", that render "layou/layout" and call "layout_controller"
    where is send to a function "layout" the object from "test_controller", see:
    Since you're already working within a framework, it is extremely unlikely
    that you'll either need or want to create another server, especially inside
    a controller. What you describe above sounds like the routing part of the
    existing server, so you should be working within that.

    I see now that you mentioned Locomotive in your original message here. I'm
    not familiar with that, but I see that it has a Google Group of its own.
    I'd suggest you ask in that group how you should be addressing your problem
    within the context of that framework. You'll likely get more appropriate
    responses there, from people who know the framework you're using.

    --
    Martin Cooper


    LayoutController.layout = function(obj)
    {
    var http = require('http'),
    fs = require('fs');
    var html = fs.readFileSync('./app/views/'+
    obj.__action+'/'+obj.fileName);
    http.createServer(function(request, response) {
    response.writeHeader(200, {"Content-Type": "text/html"});
    response.write(html);
    response.end();
    }).listen();
    obj.content = html;
    obj.render('layout/layout');
    }

    As can be seen, this function read in on single place every different obj
    that comes here, but, as you can see, the layout is all the page with css
    and javascript, it's always static, only the content changes. So, as you
    can see again, the listen obj is empty, and it's works perfectly, but when
    i put some port happens what i said before.

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rodrigo Fonseca at Dec 28, 2012 at 6:06 pm
    Ok, i just posted in there, thanks again.

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rodrigo Fonseca at Dec 31, 2012 at 7:51 pm
    hey guys,
    i just finished or almost, and i didn't need to use any other module, and
    i discovered some thing that maybe be useful to some people, here:

    LayoutController.layout = function(obj)
    {
    var fs = require('fs');
    *var html = fs.readFileSync('./app/views/'+ obj.__action+'/'+obj.fileName);*
    obj.content = html;
    //acessando a view especifica do controller layout
    obj.render('layout/layout');
    }

    I don't need to create a server just to read, i'm using this to generate my
    main layout and is working very well, i don't know if this is the right
    way, but i don't think that exist any "right way", for me, this is working
    in a way that i need...

    --
    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
    nodejs+[email protected]
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedDec 25, '12 at 3:41p
activeDec 31, '12 at 7:51p
posts15
users5
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2023 Grokbase