getting “Cannot GET /public/signup.html” error in express js











up vote
0
down vote

favorite












Very new to express and file system and don't have much idea about directories so getting this error.



var express= require('express');
var path= require('path');
var mysql= require('mysql');
var bodyParser= require('body-parser');

var app= express();
app.get('/', function(req, res) {
res.set( {
'Access-control-Allow-Origin': '*'
});
return res.redirect('/public/signup.html');
}).listen(2121);

console.log('server Running on : 2121');
app.use('/public',express.static(__dirname +"/public"));


Getting error "Cannot GET /public/signup.html"
My directories is:



-Express
--Server.js
--public
---signup.html









share|improve this question


























    up vote
    0
    down vote

    favorite












    Very new to express and file system and don't have much idea about directories so getting this error.



    var express= require('express');
    var path= require('path');
    var mysql= require('mysql');
    var bodyParser= require('body-parser');

    var app= express();
    app.get('/', function(req, res) {
    res.set( {
    'Access-control-Allow-Origin': '*'
    });
    return res.redirect('/public/signup.html');
    }).listen(2121);

    console.log('server Running on : 2121');
    app.use('/public',express.static(__dirname +"/public"));


    Getting error "Cannot GET /public/signup.html"
    My directories is:



    -Express
    --Server.js
    --public
    ---signup.html









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Very new to express and file system and don't have much idea about directories so getting this error.



      var express= require('express');
      var path= require('path');
      var mysql= require('mysql');
      var bodyParser= require('body-parser');

      var app= express();
      app.get('/', function(req, res) {
      res.set( {
      'Access-control-Allow-Origin': '*'
      });
      return res.redirect('/public/signup.html');
      }).listen(2121);

      console.log('server Running on : 2121');
      app.use('/public',express.static(__dirname +"/public"));


      Getting error "Cannot GET /public/signup.html"
      My directories is:



      -Express
      --Server.js
      --public
      ---signup.html









      share|improve this question













      Very new to express and file system and don't have much idea about directories so getting this error.



      var express= require('express');
      var path= require('path');
      var mysql= require('mysql');
      var bodyParser= require('body-parser');

      var app= express();
      app.get('/', function(req, res) {
      res.set( {
      'Access-control-Allow-Origin': '*'
      });
      return res.redirect('/public/signup.html');
      }).listen(2121);

      console.log('server Running on : 2121');
      app.use('/public',express.static(__dirname +"/public"));


      Getting error "Cannot GET /public/signup.html"
      My directories is:



      -Express
      --Server.js
      --public
      ---signup.html






      javascript node.js express






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 30 at 11:19









      WTF tom

      61




      61
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          Looks like your code is a little jumbled up. Separate out your port listener - this should always come last. Add your routes and middleware before that as individual calls to app, and also register your get request to redirect back to your server to the signup html.



          This should work:






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

          var port = 2121;
          var app = express();

          // Register Middlewares/Headers
          app.use((req, res, next) => {
          res.header("Access-Control-Allow-Origin", "*");
          next();
          });

          // Register Static
          app.use("/public", express.static(__dirname + "/public"));

          // Register redirect
          app.get('/', (req, res) => {
          res.redirect(req.baseUrl + '/public/signup.html');
          });


          app.listen(port, () => {
          console.log("server Running on : ", port);
          });








          share|improve this answer




























            up vote
            0
            down vote













            You're calling listen on app before you call use on your middleware and there are a few mistakes in your code. I think this should work:



            app
            .use('/public',express.static(`${__dirname}/public`))
            .get('/', (req, res) => {
            res.header({
            'Access-control-Allow-Origin': '*'
            });
            res.redirect(`${req.baseUrl}/public/signup.html`);
            })
            .listen(2121);





            share|improve this answer























            • But what if I want to add the directory to access multiple files later in the program.
              – WTF tom
              Aug 30 at 11:29










            • Sorry, I realised I had misunderstood what your code was doing and edited my answer. You need to call use before you call listen. I had this same issue myself a few days ago. With the edited code you should have access to the public directory no worries.
              – Michael Curry
              Aug 30 at 11:30












            • working fine....thanks
              – WTF tom
              Aug 30 at 11:48










            • No worries, make sure to mark an answer as accepted.
              – Michael Curry
              Aug 30 at 11:49


















            up vote
            0
            down vote













            You should provide



            app.use('/public',express.static(__dirname +"/public"));


            Before you using app.get



            Full example:



            var express= require('express');
            var path= require('path');
            var mysql= require('mysql');
            var bodyParser= require('body-parser');

            var app= express();
            app.use('/public',express.static(__dirname +"/public"));
            app.get('/', function(req, res) {
            res.set( {
            'Access-control-Allow-Origin': '*'
            });
            return res.redirect('/public/signup.html');
            }).listen(2121);

            console.log('server Running on : 2121');





            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',
              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%2f52095748%2fgetting-cannot-get-public-signup-html-error-in-express-js%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              Looks like your code is a little jumbled up. Separate out your port listener - this should always come last. Add your routes and middleware before that as individual calls to app, and also register your get request to redirect back to your server to the signup html.



              This should work:






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

              var port = 2121;
              var app = express();

              // Register Middlewares/Headers
              app.use((req, res, next) => {
              res.header("Access-Control-Allow-Origin", "*");
              next();
              });

              // Register Static
              app.use("/public", express.static(__dirname + "/public"));

              // Register redirect
              app.get('/', (req, res) => {
              res.redirect(req.baseUrl + '/public/signup.html');
              });


              app.listen(port, () => {
              console.log("server Running on : ", port);
              });








              share|improve this answer

























                up vote
                0
                down vote













                Looks like your code is a little jumbled up. Separate out your port listener - this should always come last. Add your routes and middleware before that as individual calls to app, and also register your get request to redirect back to your server to the signup html.



                This should work:






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

                var port = 2121;
                var app = express();

                // Register Middlewares/Headers
                app.use((req, res, next) => {
                res.header("Access-Control-Allow-Origin", "*");
                next();
                });

                // Register Static
                app.use("/public", express.static(__dirname + "/public"));

                // Register redirect
                app.get('/', (req, res) => {
                res.redirect(req.baseUrl + '/public/signup.html');
                });


                app.listen(port, () => {
                console.log("server Running on : ", port);
                });








                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Looks like your code is a little jumbled up. Separate out your port listener - this should always come last. Add your routes and middleware before that as individual calls to app, and also register your get request to redirect back to your server to the signup html.



                  This should work:






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

                  var port = 2121;
                  var app = express();

                  // Register Middlewares/Headers
                  app.use((req, res, next) => {
                  res.header("Access-Control-Allow-Origin", "*");
                  next();
                  });

                  // Register Static
                  app.use("/public", express.static(__dirname + "/public"));

                  // Register redirect
                  app.get('/', (req, res) => {
                  res.redirect(req.baseUrl + '/public/signup.html');
                  });


                  app.listen(port, () => {
                  console.log("server Running on : ", port);
                  });








                  share|improve this answer












                  Looks like your code is a little jumbled up. Separate out your port listener - this should always come last. Add your routes and middleware before that as individual calls to app, and also register your get request to redirect back to your server to the signup html.



                  This should work:






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

                  var port = 2121;
                  var app = express();

                  // Register Middlewares/Headers
                  app.use((req, res, next) => {
                  res.header("Access-Control-Allow-Origin", "*");
                  next();
                  });

                  // Register Static
                  app.use("/public", express.static(__dirname + "/public"));

                  // Register redirect
                  app.get('/', (req, res) => {
                  res.redirect(req.baseUrl + '/public/signup.html');
                  });


                  app.listen(port, () => {
                  console.log("server Running on : ", port);
                  });








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

                  var port = 2121;
                  var app = express();

                  // Register Middlewares/Headers
                  app.use((req, res, next) => {
                  res.header("Access-Control-Allow-Origin", "*");
                  next();
                  });

                  // Register Static
                  app.use("/public", express.static(__dirname + "/public"));

                  // Register redirect
                  app.get('/', (req, res) => {
                  res.redirect(req.baseUrl + '/public/signup.html');
                  });


                  app.listen(port, () => {
                  console.log("server Running on : ", port);
                  });





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

                  var port = 2121;
                  var app = express();

                  // Register Middlewares/Headers
                  app.use((req, res, next) => {
                  res.header("Access-Control-Allow-Origin", "*");
                  next();
                  });

                  // Register Static
                  app.use("/public", express.static(__dirname + "/public"));

                  // Register redirect
                  app.get('/', (req, res) => {
                  res.redirect(req.baseUrl + '/public/signup.html');
                  });


                  app.listen(port, () => {
                  console.log("server Running on : ", port);
                  });






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 30 at 11:33









                  Peak

                  845




                  845
























                      up vote
                      0
                      down vote













                      You're calling listen on app before you call use on your middleware and there are a few mistakes in your code. I think this should work:



                      app
                      .use('/public',express.static(`${__dirname}/public`))
                      .get('/', (req, res) => {
                      res.header({
                      'Access-control-Allow-Origin': '*'
                      });
                      res.redirect(`${req.baseUrl}/public/signup.html`);
                      })
                      .listen(2121);





                      share|improve this answer























                      • But what if I want to add the directory to access multiple files later in the program.
                        – WTF tom
                        Aug 30 at 11:29










                      • Sorry, I realised I had misunderstood what your code was doing and edited my answer. You need to call use before you call listen. I had this same issue myself a few days ago. With the edited code you should have access to the public directory no worries.
                        – Michael Curry
                        Aug 30 at 11:30












                      • working fine....thanks
                        – WTF tom
                        Aug 30 at 11:48










                      • No worries, make sure to mark an answer as accepted.
                        – Michael Curry
                        Aug 30 at 11:49















                      up vote
                      0
                      down vote













                      You're calling listen on app before you call use on your middleware and there are a few mistakes in your code. I think this should work:



                      app
                      .use('/public',express.static(`${__dirname}/public`))
                      .get('/', (req, res) => {
                      res.header({
                      'Access-control-Allow-Origin': '*'
                      });
                      res.redirect(`${req.baseUrl}/public/signup.html`);
                      })
                      .listen(2121);





                      share|improve this answer























                      • But what if I want to add the directory to access multiple files later in the program.
                        – WTF tom
                        Aug 30 at 11:29










                      • Sorry, I realised I had misunderstood what your code was doing and edited my answer. You need to call use before you call listen. I had this same issue myself a few days ago. With the edited code you should have access to the public directory no worries.
                        – Michael Curry
                        Aug 30 at 11:30












                      • working fine....thanks
                        – WTF tom
                        Aug 30 at 11:48










                      • No worries, make sure to mark an answer as accepted.
                        – Michael Curry
                        Aug 30 at 11:49













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      You're calling listen on app before you call use on your middleware and there are a few mistakes in your code. I think this should work:



                      app
                      .use('/public',express.static(`${__dirname}/public`))
                      .get('/', (req, res) => {
                      res.header({
                      'Access-control-Allow-Origin': '*'
                      });
                      res.redirect(`${req.baseUrl}/public/signup.html`);
                      })
                      .listen(2121);





                      share|improve this answer














                      You're calling listen on app before you call use on your middleware and there are a few mistakes in your code. I think this should work:



                      app
                      .use('/public',express.static(`${__dirname}/public`))
                      .get('/', (req, res) => {
                      res.header({
                      'Access-control-Allow-Origin': '*'
                      });
                      res.redirect(`${req.baseUrl}/public/signup.html`);
                      })
                      .listen(2121);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 30 at 11:50

























                      answered Aug 30 at 11:24









                      Michael Curry

                      624518




                      624518












                      • But what if I want to add the directory to access multiple files later in the program.
                        – WTF tom
                        Aug 30 at 11:29










                      • Sorry, I realised I had misunderstood what your code was doing and edited my answer. You need to call use before you call listen. I had this same issue myself a few days ago. With the edited code you should have access to the public directory no worries.
                        – Michael Curry
                        Aug 30 at 11:30












                      • working fine....thanks
                        – WTF tom
                        Aug 30 at 11:48










                      • No worries, make sure to mark an answer as accepted.
                        – Michael Curry
                        Aug 30 at 11:49


















                      • But what if I want to add the directory to access multiple files later in the program.
                        – WTF tom
                        Aug 30 at 11:29










                      • Sorry, I realised I had misunderstood what your code was doing and edited my answer. You need to call use before you call listen. I had this same issue myself a few days ago. With the edited code you should have access to the public directory no worries.
                        – Michael Curry
                        Aug 30 at 11:30












                      • working fine....thanks
                        – WTF tom
                        Aug 30 at 11:48










                      • No worries, make sure to mark an answer as accepted.
                        – Michael Curry
                        Aug 30 at 11:49
















                      But what if I want to add the directory to access multiple files later in the program.
                      – WTF tom
                      Aug 30 at 11:29




                      But what if I want to add the directory to access multiple files later in the program.
                      – WTF tom
                      Aug 30 at 11:29












                      Sorry, I realised I had misunderstood what your code was doing and edited my answer. You need to call use before you call listen. I had this same issue myself a few days ago. With the edited code you should have access to the public directory no worries.
                      – Michael Curry
                      Aug 30 at 11:30






                      Sorry, I realised I had misunderstood what your code was doing and edited my answer. You need to call use before you call listen. I had this same issue myself a few days ago. With the edited code you should have access to the public directory no worries.
                      – Michael Curry
                      Aug 30 at 11:30














                      working fine....thanks
                      – WTF tom
                      Aug 30 at 11:48




                      working fine....thanks
                      – WTF tom
                      Aug 30 at 11:48












                      No worries, make sure to mark an answer as accepted.
                      – Michael Curry
                      Aug 30 at 11:49




                      No worries, make sure to mark an answer as accepted.
                      – Michael Curry
                      Aug 30 at 11:49










                      up vote
                      0
                      down vote













                      You should provide



                      app.use('/public',express.static(__dirname +"/public"));


                      Before you using app.get



                      Full example:



                      var express= require('express');
                      var path= require('path');
                      var mysql= require('mysql');
                      var bodyParser= require('body-parser');

                      var app= express();
                      app.use('/public',express.static(__dirname +"/public"));
                      app.get('/', function(req, res) {
                      res.set( {
                      'Access-control-Allow-Origin': '*'
                      });
                      return res.redirect('/public/signup.html');
                      }).listen(2121);

                      console.log('server Running on : 2121');





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        You should provide



                        app.use('/public',express.static(__dirname +"/public"));


                        Before you using app.get



                        Full example:



                        var express= require('express');
                        var path= require('path');
                        var mysql= require('mysql');
                        var bodyParser= require('body-parser');

                        var app= express();
                        app.use('/public',express.static(__dirname +"/public"));
                        app.get('/', function(req, res) {
                        res.set( {
                        'Access-control-Allow-Origin': '*'
                        });
                        return res.redirect('/public/signup.html');
                        }).listen(2121);

                        console.log('server Running on : 2121');





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You should provide



                          app.use('/public',express.static(__dirname +"/public"));


                          Before you using app.get



                          Full example:



                          var express= require('express');
                          var path= require('path');
                          var mysql= require('mysql');
                          var bodyParser= require('body-parser');

                          var app= express();
                          app.use('/public',express.static(__dirname +"/public"));
                          app.get('/', function(req, res) {
                          res.set( {
                          'Access-control-Allow-Origin': '*'
                          });
                          return res.redirect('/public/signup.html');
                          }).listen(2121);

                          console.log('server Running on : 2121');





                          share|improve this answer














                          You should provide



                          app.use('/public',express.static(__dirname +"/public"));


                          Before you using app.get



                          Full example:



                          var express= require('express');
                          var path= require('path');
                          var mysql= require('mysql');
                          var bodyParser= require('body-parser');

                          var app= express();
                          app.use('/public',express.static(__dirname +"/public"));
                          app.get('/', function(req, res) {
                          res.set( {
                          'Access-control-Allow-Origin': '*'
                          });
                          return res.redirect('/public/signup.html');
                          }).listen(2121);

                          console.log('server Running on : 2121');






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 20 at 16:04









                          Yvette Colomb

                          19.8k1369107




                          19.8k1369107










                          answered Aug 30 at 11:53









                          аlex dykyі

                          1,3311023




                          1,3311023






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f52095748%2fgetting-cannot-get-public-signup-html-error-in-express-js%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

                              Create new schema in PostgreSQL using DBeaver

                              Deepest pit of an array with Javascript: test on Codility

                              Costa Masnaga