Irfan Irfan - 26 days ago 5
Node.js Question

Start nodejs in browser form begining?

I am new in Nodejs and using Mac OS (MAMP, localhost:8888), I have already installed and I can execute programs in terminal. But how to in build with html. Can, we include nodejs as a external library like (jQuery, Bootstrap).

Answer

Please refer this Hello world example. node.js

var http = require("http");
var server = http.createServer(handler);

var fs = require('fs');

server.listen(3003)

function handler(req,resp){

    fs.readFile("index.html",function(err,data){
        if(err){
            console.log("error in  loading file.");
            resp.end("failed to load")
        }else{
            resp.writeHead(200);
            resp.end(data)
        }
    })
}


HTML

<html>
<head>
</head>
<body>
    Hello world.!!
</body>
</html>

Once you mentioned port number in listen function,
node server will run on that port.
So until you are not giving same port number as Apache,you can run both Apache and nodejs parallel.
If your Apache and nodejs server sharing same port number , then you need to stop one of them in order to use other.

Your Question :

Can, we include nodejs as a external library like (jQuery, Bootstrap).

Node.js is not library.So you can not include it like jQuery or bootstrap.
It is platform for javascript to run on server side.
And using nodejs we can create Server which serve your content of Web like Apache do.

When you say node app.js (insead of app.js it can be any name) from your command prompt,you starting your node server.

In above example index.html you can include your jQuery or angular or bootstrap library as you do when using Apache

Comments