Related Post

40 Replies to “Node.js Tutorial for Beginners – 13 – Creating a Basic Server”

  1. I feel so silly, was trying to find out why my server kept processing with exit code 0. My configuration was still set to app.js! XD

  2. How to run multiple instance of http server in node js. If i am connecting more than one user it is getting hang. Why it is so?

  3. Little late, but this video was great! The following two really helped out as well with an assignment I was working with!

  4. Cool tutorial but this didn't work for me.
    I had to use .on('request' onRequest) method
    and put it between .createServer() and .listen(XXXX)
    to get the text response in the browser.

  5. var http = require('http');

    // we require the http Core Module for making a basic HTTP Web Server.

    function onRequest(request, response) {

    console.log(" a user made a request : " + request.url);
    response.writeHeader(200, {"Content-Type" :"text/plain"});
    response.write("Here's some data");
    response.end();

    }
    // request.url tells us what the Requestee is trying to access. in this case you'll get a '/' which means the user is trying to access your homepage or the v.first, initial page of your site.

    // response.writeHeader basically writes the preface for your response. here it takes 200 as the status code, which means everything went OK. Content-Type tells what type of data will be sent in response to the user's request. Here it'll be a plain text. in case you're sending, say a html file (to be displayed as a webpage in user's browser), it'll be {"Content-Type : "text/html"}.

    //response.write("response content") includes the content you'll send in the response. here, it's simple text. In case of an HTML file, you'll have to send a data stream ( you'll first need to read in the file using fs.createReadStream("filename with directory").pipe(response), then piping it out as a response using the pipe method. ) which is explained in the next tutorial.

    //response.end() basically delineates the end of your response.

    http.createServer(onRequest).listen(8888);
    // we call a HTTP method called createServer(onRequest). what this does, is whenever a User tries to connect to your website, it called the method mentioned in the parentheses that follow the createServer word. The listen property/method (i am not clear on this 3 dot syntax) basically specifies at what port your webserver will listen to, for potential and prospective requests.

    console.log("server is running");
    // just put in there to signify to user that server has started without any problems.

  6. Finally got understood what that "res" and "req" mean from other tutorials… btw one question:

    you put console.log("server is running") after http.createSever() to indicate the server is running, does it mean that if http.createSever() throws an error and doesn't create the server, the "compiler"(yea right?) won't go on to execute console.log("server is running")? In my practice, I noticed that whenever JS meets a line of code that isn't proper to run it will simply just skip that… correct me if I'm wrong. Thanks

  7. I realise that you do not read the comments, but nice stuff. One thing, though, you really need to stop saying "log this out", when you mean to say "write a log message". You are over-subscribing the term 'log out', which really means something entirely different.

  8. I get "Process finished with exit code 0" – SERVER DOESN'T RUN , even after I copy pasted the exact code from NewBoston GitHub. Any suggestions ( using Webstorm )?

  9. hi,
    how can l do if l want to send a javascript page l did change content type : text/javascript , but it didn't nothing. it sent the javascript itself Thanks a lot

  10. Server Status/Code
    200 OK
    304 Not Modified
    401 Unauthorized
    403 Forbidden
    404 Not Found
    500 Internal Server Error
    503 Service Unavailable

  11. The Best Tutorial on Node out there. Thanks, It helped a lot ! It's amazing especially for beginners. You have a great way of teaching things.

  12. Woow! This tutorial is awesome! for two days i've been watching all bunch of stuff for hours and i did not understand anything but after this tutorial i know what node.js is, thank you very much

  13. hey Bucky, thanks for the tutorial. I am also using IntelliJ but for some reason auto-complete does not offer "writeHead" method for the "response" object. It works fine after I hard coded. Additionally, methods writeHead and createServer are underlined with a wavy line. Much appreciate for the response on this matter. Thanks.

  14. Hi,I am using sublime text 3 for this tutorial as it is convenient for me to type and also due to a lot of cool features.I have written the same code for this creating server tutorial.I get the message in the Console as "Server has started" but there on no output in the log and also in the browser I get server not found. :(Please guide me, do I need to download some package?.Thanks in advance!.

  15. If every single time you refresh the webpage, onRequest() run twice, so how come "Here is some data" only printed once on web page? Is it suppose to be twice???

  16. Why my node.js does not have require() function ! It is saying Unresolved function ! I am using student version, is this the reason ??? Thanks for your video,.

  17. How exactly will I be loading the style.css in the handleRequest function? WIll I have to write a seperat function for node to also pull the style.css???

Leave a Reply

Your email address will not be published. Required fields are marked *