electronjs - remote access nodejs










0















I have a very basic setup in my electron.js. then I have a js file that link directly to index.html:



app.js



 const http = require('http');
var url = require('url');
var fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
http.createServer(function (req, res)
var q = url.parse(req.url, true);
var filename = "example.html";
fs.readFile(filename, function(err, data)
res.writeHead(200, 'Content-Type': 'text/html');
res.write(data);
return res.end();
);
).listen(port, hostname,()=>
console.log(`Server running at http://$hostname:$port/`);
);


So far I can access to example.html by going to localhost:3000, using the same computer.



But I want to use other device to connect to this example.html. So I thought it should be straight forward. First, I need to find out the local IP:



var os = require('os');
var addresses = ;
for (var k in interfaces)
for (var k2 in interfaces[k])
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal)
addresses.push(address.address);



console.log(addresses);


I get 192.168.0.200, which is the ip my wifi router provide to my computer. Then, I try access to example.html by browser with URL 192.168.0.200:3000, The browser cannot find the page.



Is there anything missing?










share|improve this question

















  • 1





    Your app is listening on 127.0.0.1

    – tkausl
    Nov 16 '18 at 8:32











  • that is localhost, and sure you can access it using the same computer with that. however, if you want to use other device to access this, you need to find the IP that is provided by the wifi router, which is 192.168.0.200. But somehow that did not work.

    – sooon
    Nov 16 '18 at 17:23











  • @tkausl actually, you are close to the answer. check my answer below:

    – sooon
    Nov 17 '18 at 6:55















0















I have a very basic setup in my electron.js. then I have a js file that link directly to index.html:



app.js



 const http = require('http');
var url = require('url');
var fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
http.createServer(function (req, res)
var q = url.parse(req.url, true);
var filename = "example.html";
fs.readFile(filename, function(err, data)
res.writeHead(200, 'Content-Type': 'text/html');
res.write(data);
return res.end();
);
).listen(port, hostname,()=>
console.log(`Server running at http://$hostname:$port/`);
);


So far I can access to example.html by going to localhost:3000, using the same computer.



But I want to use other device to connect to this example.html. So I thought it should be straight forward. First, I need to find out the local IP:



var os = require('os');
var addresses = ;
for (var k in interfaces)
for (var k2 in interfaces[k])
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal)
addresses.push(address.address);



console.log(addresses);


I get 192.168.0.200, which is the ip my wifi router provide to my computer. Then, I try access to example.html by browser with URL 192.168.0.200:3000, The browser cannot find the page.



Is there anything missing?










share|improve this question

















  • 1





    Your app is listening on 127.0.0.1

    – tkausl
    Nov 16 '18 at 8:32











  • that is localhost, and sure you can access it using the same computer with that. however, if you want to use other device to access this, you need to find the IP that is provided by the wifi router, which is 192.168.0.200. But somehow that did not work.

    – sooon
    Nov 16 '18 at 17:23











  • @tkausl actually, you are close to the answer. check my answer below:

    – sooon
    Nov 17 '18 at 6:55













0












0








0








I have a very basic setup in my electron.js. then I have a js file that link directly to index.html:



app.js



 const http = require('http');
var url = require('url');
var fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
http.createServer(function (req, res)
var q = url.parse(req.url, true);
var filename = "example.html";
fs.readFile(filename, function(err, data)
res.writeHead(200, 'Content-Type': 'text/html');
res.write(data);
return res.end();
);
).listen(port, hostname,()=>
console.log(`Server running at http://$hostname:$port/`);
);


So far I can access to example.html by going to localhost:3000, using the same computer.



But I want to use other device to connect to this example.html. So I thought it should be straight forward. First, I need to find out the local IP:



var os = require('os');
var addresses = ;
for (var k in interfaces)
for (var k2 in interfaces[k])
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal)
addresses.push(address.address);



console.log(addresses);


I get 192.168.0.200, which is the ip my wifi router provide to my computer. Then, I try access to example.html by browser with URL 192.168.0.200:3000, The browser cannot find the page.



Is there anything missing?










share|improve this question














I have a very basic setup in my electron.js. then I have a js file that link directly to index.html:



app.js



 const http = require('http');
var url = require('url');
var fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
http.createServer(function (req, res)
var q = url.parse(req.url, true);
var filename = "example.html";
fs.readFile(filename, function(err, data)
res.writeHead(200, 'Content-Type': 'text/html');
res.write(data);
return res.end();
);
).listen(port, hostname,()=>
console.log(`Server running at http://$hostname:$port/`);
);


So far I can access to example.html by going to localhost:3000, using the same computer.



But I want to use other device to connect to this example.html. So I thought it should be straight forward. First, I need to find out the local IP:



var os = require('os');
var addresses = ;
for (var k in interfaces)
for (var k2 in interfaces[k])
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal)
addresses.push(address.address);



console.log(addresses);


I get 192.168.0.200, which is the ip my wifi router provide to my computer. Then, I try access to example.html by browser with URL 192.168.0.200:3000, The browser cannot find the page.



Is there anything missing?







node.js electron






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 8:26









sooonsooon

1,69633369




1,69633369







  • 1





    Your app is listening on 127.0.0.1

    – tkausl
    Nov 16 '18 at 8:32











  • that is localhost, and sure you can access it using the same computer with that. however, if you want to use other device to access this, you need to find the IP that is provided by the wifi router, which is 192.168.0.200. But somehow that did not work.

    – sooon
    Nov 16 '18 at 17:23











  • @tkausl actually, you are close to the answer. check my answer below:

    – sooon
    Nov 17 '18 at 6:55












  • 1





    Your app is listening on 127.0.0.1

    – tkausl
    Nov 16 '18 at 8:32











  • that is localhost, and sure you can access it using the same computer with that. however, if you want to use other device to access this, you need to find the IP that is provided by the wifi router, which is 192.168.0.200. But somehow that did not work.

    – sooon
    Nov 16 '18 at 17:23











  • @tkausl actually, you are close to the answer. check my answer below:

    – sooon
    Nov 17 '18 at 6:55







1




1





Your app is listening on 127.0.0.1

– tkausl
Nov 16 '18 at 8:32





Your app is listening on 127.0.0.1

– tkausl
Nov 16 '18 at 8:32













that is localhost, and sure you can access it using the same computer with that. however, if you want to use other device to access this, you need to find the IP that is provided by the wifi router, which is 192.168.0.200. But somehow that did not work.

– sooon
Nov 16 '18 at 17:23





that is localhost, and sure you can access it using the same computer with that. however, if you want to use other device to access this, you need to find the IP that is provided by the wifi router, which is 192.168.0.200. But somehow that did not work.

– sooon
Nov 16 '18 at 17:23













@tkausl actually, you are close to the answer. check my answer below:

– sooon
Nov 17 '18 at 6:55





@tkausl actually, you are close to the answer. check my answer below:

– sooon
Nov 17 '18 at 6:55












1 Answer
1






active

oldest

votes


















0














As it turn out, it is rather straight forward.
I just have to replace 127.0.0.1 with whichever IP router provided.



///get the ip from the router
var os = require('os');
var addresses = ;
for (var k in interfaces)
for (var k2 in interfaces[k])
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal)
addresses.push(address.address);



console.log(addresses);///<-- addresses is an array

const http = require('http');
var url = require('url');
var fs = require('fs');
const hostname = addresses[0];///<-- first element of addresses
const port = 3000;
http.createServer(function (req, res)
var q = url.parse(req.url, true);
var filename = "example.html";
fs.readFile(filename, function(err, data)
res.writeHead(200, 'Content-Type': 'text/html');
res.write(data);
return res.end();
);
).listen(port, hostname,()=>
console.log(`Server running at http://$hostname:$port/`);
);


then, you can do 192.168.0.200:3000/example.html from any device.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333993%2felectronjs-remote-access-nodejs%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As it turn out, it is rather straight forward.
    I just have to replace 127.0.0.1 with whichever IP router provided.



    ///get the ip from the router
    var os = require('os');
    var addresses = ;
    for (var k in interfaces)
    for (var k2 in interfaces[k])
    var address = interfaces[k][k2];
    if (address.family === 'IPv4' && !address.internal)
    addresses.push(address.address);



    console.log(addresses);///<-- addresses is an array

    const http = require('http');
    var url = require('url');
    var fs = require('fs');
    const hostname = addresses[0];///<-- first element of addresses
    const port = 3000;
    http.createServer(function (req, res)
    var q = url.parse(req.url, true);
    var filename = "example.html";
    fs.readFile(filename, function(err, data)
    res.writeHead(200, 'Content-Type': 'text/html');
    res.write(data);
    return res.end();
    );
    ).listen(port, hostname,()=>
    console.log(`Server running at http://$hostname:$port/`);
    );


    then, you can do 192.168.0.200:3000/example.html from any device.






    share|improve this answer



























      0














      As it turn out, it is rather straight forward.
      I just have to replace 127.0.0.1 with whichever IP router provided.



      ///get the ip from the router
      var os = require('os');
      var addresses = ;
      for (var k in interfaces)
      for (var k2 in interfaces[k])
      var address = interfaces[k][k2];
      if (address.family === 'IPv4' && !address.internal)
      addresses.push(address.address);



      console.log(addresses);///<-- addresses is an array

      const http = require('http');
      var url = require('url');
      var fs = require('fs');
      const hostname = addresses[0];///<-- first element of addresses
      const port = 3000;
      http.createServer(function (req, res)
      var q = url.parse(req.url, true);
      var filename = "example.html";
      fs.readFile(filename, function(err, data)
      res.writeHead(200, 'Content-Type': 'text/html');
      res.write(data);
      return res.end();
      );
      ).listen(port, hostname,()=>
      console.log(`Server running at http://$hostname:$port/`);
      );


      then, you can do 192.168.0.200:3000/example.html from any device.






      share|improve this answer

























        0












        0








        0







        As it turn out, it is rather straight forward.
        I just have to replace 127.0.0.1 with whichever IP router provided.



        ///get the ip from the router
        var os = require('os');
        var addresses = ;
        for (var k in interfaces)
        for (var k2 in interfaces[k])
        var address = interfaces[k][k2];
        if (address.family === 'IPv4' && !address.internal)
        addresses.push(address.address);



        console.log(addresses);///<-- addresses is an array

        const http = require('http');
        var url = require('url');
        var fs = require('fs');
        const hostname = addresses[0];///<-- first element of addresses
        const port = 3000;
        http.createServer(function (req, res)
        var q = url.parse(req.url, true);
        var filename = "example.html";
        fs.readFile(filename, function(err, data)
        res.writeHead(200, 'Content-Type': 'text/html');
        res.write(data);
        return res.end();
        );
        ).listen(port, hostname,()=>
        console.log(`Server running at http://$hostname:$port/`);
        );


        then, you can do 192.168.0.200:3000/example.html from any device.






        share|improve this answer













        As it turn out, it is rather straight forward.
        I just have to replace 127.0.0.1 with whichever IP router provided.



        ///get the ip from the router
        var os = require('os');
        var addresses = ;
        for (var k in interfaces)
        for (var k2 in interfaces[k])
        var address = interfaces[k][k2];
        if (address.family === 'IPv4' && !address.internal)
        addresses.push(address.address);



        console.log(addresses);///<-- addresses is an array

        const http = require('http');
        var url = require('url');
        var fs = require('fs');
        const hostname = addresses[0];///<-- first element of addresses
        const port = 3000;
        http.createServer(function (req, res)
        var q = url.parse(req.url, true);
        var filename = "example.html";
        fs.readFile(filename, function(err, data)
        res.writeHead(200, 'Content-Type': 'text/html');
        res.write(data);
        return res.end();
        );
        ).listen(port, hostname,()=>
        console.log(`Server running at http://$hostname:$port/`);
        );


        then, you can do 192.168.0.200:3000/example.html from any device.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 7:00









        sooonsooon

        1,69633369




        1,69633369





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333993%2felectronjs-remote-access-nodejs%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線