How to use Electron with an existing Express application





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I read and tried samples like the one on https://github.com/frankhale/electron-with-express but still I don't understand how to convert an Express app into Electron



How can I use Electron with an existing Express application?



Take for example this Express application:



app.js



var express = require("express");
var app = express();

var request = require("request");

app.set("view engine", "ejs");

app.get("/", function(req, res) {
res.render("search");
});

app.get("/results", function(req, res){
var query = req.query.search;
var url = "https://yts.am/api/v2/list_movies.json?sort=seeds&limit=15&query_term='" + query + "'";
request(url, function(error, response, body){
var data = JSON.parse(body);
if(!error && response.statusCode == 200){
//res.send(data["data"]["movies"][0]["title"]);
res.render("results", {data: data});
//["movies"][0]["title"]
}
else
console.log(data);
});
});

app.listen(process.env.PORT, process.env.IP, function(){
console.log("IMDB server has started");
});


search.ejs



Search for a movie

<form action="results" method="GET">
<input type="text" placeholder="search term" name="search">
<input type="submit">
</form>


results.ejs



Results page
<ul>
<% data["data"]["movies"].forEach(function(movie){ %>
<li>
<strong><%= movie["title"]%></strong> - <%= movie["year"]%>
</li>
<% }) %>
</ul>

<a href="/">Search again</a>









share|improve this question































    3















    I read and tried samples like the one on https://github.com/frankhale/electron-with-express but still I don't understand how to convert an Express app into Electron



    How can I use Electron with an existing Express application?



    Take for example this Express application:



    app.js



    var express = require("express");
    var app = express();

    var request = require("request");

    app.set("view engine", "ejs");

    app.get("/", function(req, res) {
    res.render("search");
    });

    app.get("/results", function(req, res){
    var query = req.query.search;
    var url = "https://yts.am/api/v2/list_movies.json?sort=seeds&limit=15&query_term='" + query + "'";
    request(url, function(error, response, body){
    var data = JSON.parse(body);
    if(!error && response.statusCode == 200){
    //res.send(data["data"]["movies"][0]["title"]);
    res.render("results", {data: data});
    //["movies"][0]["title"]
    }
    else
    console.log(data);
    });
    });

    app.listen(process.env.PORT, process.env.IP, function(){
    console.log("IMDB server has started");
    });


    search.ejs



    Search for a movie

    <form action="results" method="GET">
    <input type="text" placeholder="search term" name="search">
    <input type="submit">
    </form>


    results.ejs



    Results page
    <ul>
    <% data["data"]["movies"].forEach(function(movie){ %>
    <li>
    <strong><%= movie["title"]%></strong> - <%= movie["year"]%>
    </li>
    <% }) %>
    </ul>

    <a href="/">Search again</a>









    share|improve this question



























      3












      3








      3








      I read and tried samples like the one on https://github.com/frankhale/electron-with-express but still I don't understand how to convert an Express app into Electron



      How can I use Electron with an existing Express application?



      Take for example this Express application:



      app.js



      var express = require("express");
      var app = express();

      var request = require("request");

      app.set("view engine", "ejs");

      app.get("/", function(req, res) {
      res.render("search");
      });

      app.get("/results", function(req, res){
      var query = req.query.search;
      var url = "https://yts.am/api/v2/list_movies.json?sort=seeds&limit=15&query_term='" + query + "'";
      request(url, function(error, response, body){
      var data = JSON.parse(body);
      if(!error && response.statusCode == 200){
      //res.send(data["data"]["movies"][0]["title"]);
      res.render("results", {data: data});
      //["movies"][0]["title"]
      }
      else
      console.log(data);
      });
      });

      app.listen(process.env.PORT, process.env.IP, function(){
      console.log("IMDB server has started");
      });


      search.ejs



      Search for a movie

      <form action="results" method="GET">
      <input type="text" placeholder="search term" name="search">
      <input type="submit">
      </form>


      results.ejs



      Results page
      <ul>
      <% data["data"]["movies"].forEach(function(movie){ %>
      <li>
      <strong><%= movie["title"]%></strong> - <%= movie["year"]%>
      </li>
      <% }) %>
      </ul>

      <a href="/">Search again</a>









      share|improve this question
















      I read and tried samples like the one on https://github.com/frankhale/electron-with-express but still I don't understand how to convert an Express app into Electron



      How can I use Electron with an existing Express application?



      Take for example this Express application:



      app.js



      var express = require("express");
      var app = express();

      var request = require("request");

      app.set("view engine", "ejs");

      app.get("/", function(req, res) {
      res.render("search");
      });

      app.get("/results", function(req, res){
      var query = req.query.search;
      var url = "https://yts.am/api/v2/list_movies.json?sort=seeds&limit=15&query_term='" + query + "'";
      request(url, function(error, response, body){
      var data = JSON.parse(body);
      if(!error && response.statusCode == 200){
      //res.send(data["data"]["movies"][0]["title"]);
      res.render("results", {data: data});
      //["movies"][0]["title"]
      }
      else
      console.log(data);
      });
      });

      app.listen(process.env.PORT, process.env.IP, function(){
      console.log("IMDB server has started");
      });


      search.ejs



      Search for a movie

      <form action="results" method="GET">
      <input type="text" placeholder="search term" name="search">
      <input type="submit">
      </form>


      results.ejs



      Results page
      <ul>
      <% data["data"]["movies"].forEach(function(movie){ %>
      <li>
      <strong><%= movie["title"]%></strong> - <%= movie["year"]%>
      </li>
      <% }) %>
      </ul>

      <a href="/">Search again</a>






      node.js electron






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 23:46









      Tiny Giant

      13.6k64358




      13.6k64358










      asked Nov 16 '18 at 23:11









      fedetekafedeteka

      380319




      380319
























          1 Answer
          1






          active

          oldest

          votes


















          5














          In the Electron main.js file, you should require your app.js file to start the Express application, then create a new instance of BrowserWindow and load the URL that your Express application is listening on.



          Note that you'll either have to hard code the IP and PORT in Electron, or export them from Express and import them into the Electron main.js script.



          ./main.js



          const { BrowserWindow, app } = require('electron')
          require('./app.js')

          let mainWindow = null

          function main() {
          mainWindow = new BrowserWindow()
          mainWindow.loadURL(`http://localhost:3000/`)
          mainWindow.on('close', event => {
          mainWindow = null
          })
          }

          app.on('ready', main)


          ./package.json



          {
          "name": "your-app-name",
          "version": "1.0.0",
          "description": "A description of your application",
          "main": "main.js",
          "scripts": {
          "start": "electron ."
          },
          "repository": "https://github.com/link/to/repo",
          "keywords": [ "some","keywords" ],
          "author": "You",
          "license": "Your License",
          "dependencies": {
          "ejs": "^2.6.1", // required for your specific example where `ejs` package is used
          "electron": "^3.0.9",
          "express": "^4.16.4"
          }
          }


          Then you'll want to make sure that the node_modules directory is in the same directory as main.js and package.json.



          Finally, you can start your Express/Electron application using:



          > npm start




          If your Express application is not in the same directory as Electron, you will have to set the views directory for express accordingly:



          app.js



          var path = require('path')
          var request = require("request");
          var express = require("express");
          var app = express();

          app.set('views', path.join(__dirname, '/views'));

          ...


          Where /views is a directory relative to app.js that contains your views.






          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%2f53346562%2fhow-to-use-electron-with-an-existing-express-application%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









            5














            In the Electron main.js file, you should require your app.js file to start the Express application, then create a new instance of BrowserWindow and load the URL that your Express application is listening on.



            Note that you'll either have to hard code the IP and PORT in Electron, or export them from Express and import them into the Electron main.js script.



            ./main.js



            const { BrowserWindow, app } = require('electron')
            require('./app.js')

            let mainWindow = null

            function main() {
            mainWindow = new BrowserWindow()
            mainWindow.loadURL(`http://localhost:3000/`)
            mainWindow.on('close', event => {
            mainWindow = null
            })
            }

            app.on('ready', main)


            ./package.json



            {
            "name": "your-app-name",
            "version": "1.0.0",
            "description": "A description of your application",
            "main": "main.js",
            "scripts": {
            "start": "electron ."
            },
            "repository": "https://github.com/link/to/repo",
            "keywords": [ "some","keywords" ],
            "author": "You",
            "license": "Your License",
            "dependencies": {
            "ejs": "^2.6.1", // required for your specific example where `ejs` package is used
            "electron": "^3.0.9",
            "express": "^4.16.4"
            }
            }


            Then you'll want to make sure that the node_modules directory is in the same directory as main.js and package.json.



            Finally, you can start your Express/Electron application using:



            > npm start




            If your Express application is not in the same directory as Electron, you will have to set the views directory for express accordingly:



            app.js



            var path = require('path')
            var request = require("request");
            var express = require("express");
            var app = express();

            app.set('views', path.join(__dirname, '/views'));

            ...


            Where /views is a directory relative to app.js that contains your views.






            share|improve this answer






























              5














              In the Electron main.js file, you should require your app.js file to start the Express application, then create a new instance of BrowserWindow and load the URL that your Express application is listening on.



              Note that you'll either have to hard code the IP and PORT in Electron, or export them from Express and import them into the Electron main.js script.



              ./main.js



              const { BrowserWindow, app } = require('electron')
              require('./app.js')

              let mainWindow = null

              function main() {
              mainWindow = new BrowserWindow()
              mainWindow.loadURL(`http://localhost:3000/`)
              mainWindow.on('close', event => {
              mainWindow = null
              })
              }

              app.on('ready', main)


              ./package.json



              {
              "name": "your-app-name",
              "version": "1.0.0",
              "description": "A description of your application",
              "main": "main.js",
              "scripts": {
              "start": "electron ."
              },
              "repository": "https://github.com/link/to/repo",
              "keywords": [ "some","keywords" ],
              "author": "You",
              "license": "Your License",
              "dependencies": {
              "ejs": "^2.6.1", // required for your specific example where `ejs` package is used
              "electron": "^3.0.9",
              "express": "^4.16.4"
              }
              }


              Then you'll want to make sure that the node_modules directory is in the same directory as main.js and package.json.



              Finally, you can start your Express/Electron application using:



              > npm start




              If your Express application is not in the same directory as Electron, you will have to set the views directory for express accordingly:



              app.js



              var path = require('path')
              var request = require("request");
              var express = require("express");
              var app = express();

              app.set('views', path.join(__dirname, '/views'));

              ...


              Where /views is a directory relative to app.js that contains your views.






              share|improve this answer




























                5












                5








                5







                In the Electron main.js file, you should require your app.js file to start the Express application, then create a new instance of BrowserWindow and load the URL that your Express application is listening on.



                Note that you'll either have to hard code the IP and PORT in Electron, or export them from Express and import them into the Electron main.js script.



                ./main.js



                const { BrowserWindow, app } = require('electron')
                require('./app.js')

                let mainWindow = null

                function main() {
                mainWindow = new BrowserWindow()
                mainWindow.loadURL(`http://localhost:3000/`)
                mainWindow.on('close', event => {
                mainWindow = null
                })
                }

                app.on('ready', main)


                ./package.json



                {
                "name": "your-app-name",
                "version": "1.0.0",
                "description": "A description of your application",
                "main": "main.js",
                "scripts": {
                "start": "electron ."
                },
                "repository": "https://github.com/link/to/repo",
                "keywords": [ "some","keywords" ],
                "author": "You",
                "license": "Your License",
                "dependencies": {
                "ejs": "^2.6.1", // required for your specific example where `ejs` package is used
                "electron": "^3.0.9",
                "express": "^4.16.4"
                }
                }


                Then you'll want to make sure that the node_modules directory is in the same directory as main.js and package.json.



                Finally, you can start your Express/Electron application using:



                > npm start




                If your Express application is not in the same directory as Electron, you will have to set the views directory for express accordingly:



                app.js



                var path = require('path')
                var request = require("request");
                var express = require("express");
                var app = express();

                app.set('views', path.join(__dirname, '/views'));

                ...


                Where /views is a directory relative to app.js that contains your views.






                share|improve this answer















                In the Electron main.js file, you should require your app.js file to start the Express application, then create a new instance of BrowserWindow and load the URL that your Express application is listening on.



                Note that you'll either have to hard code the IP and PORT in Electron, or export them from Express and import them into the Electron main.js script.



                ./main.js



                const { BrowserWindow, app } = require('electron')
                require('./app.js')

                let mainWindow = null

                function main() {
                mainWindow = new BrowserWindow()
                mainWindow.loadURL(`http://localhost:3000/`)
                mainWindow.on('close', event => {
                mainWindow = null
                })
                }

                app.on('ready', main)


                ./package.json



                {
                "name": "your-app-name",
                "version": "1.0.0",
                "description": "A description of your application",
                "main": "main.js",
                "scripts": {
                "start": "electron ."
                },
                "repository": "https://github.com/link/to/repo",
                "keywords": [ "some","keywords" ],
                "author": "You",
                "license": "Your License",
                "dependencies": {
                "ejs": "^2.6.1", // required for your specific example where `ejs` package is used
                "electron": "^3.0.9",
                "express": "^4.16.4"
                }
                }


                Then you'll want to make sure that the node_modules directory is in the same directory as main.js and package.json.



                Finally, you can start your Express/Electron application using:



                > npm start




                If your Express application is not in the same directory as Electron, you will have to set the views directory for express accordingly:



                app.js



                var path = require('path')
                var request = require("request");
                var express = require("express");
                var app = express();

                app.set('views', path.join(__dirname, '/views'));

                ...


                Where /views is a directory relative to app.js that contains your views.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 17 '18 at 16:12

























                answered Nov 16 '18 at 23:26









                Tiny GiantTiny Giant

                13.6k64358




                13.6k64358
































                    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%2f53346562%2fhow-to-use-electron-with-an-existing-express-application%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

                    Bressuire

                    Vorschmack

                    Quarantine