Skip to content
Advertisement

Making a signup form and login form using express server

So I am basically making a simple website on my localhost that has a signup form and some other html elements. I managed to setup the signup process smoothly. So when the user fills in the form and submits it to the root route(“/”), an email is sent to the subscriber containing a random password using nodemailer and then the password is to be used to login to the user dashboard. The whole data is submitted as a mongodb document which I had setup. Upto this the process goes smoothly and when the user does the signup, the mail is sent to the subscriber’s email id and then they are redirected to the login page. So now I am confused about setting up the server.

    // index.js file
    
    const express = require("express");

    const app = express();
    const port = 5000;

    const mainApp = require("./main.js");
    const loginApp = require("./login.js");

    app
      .use(function (req, res, next) {
        if (req.hostname == "http://localhost:5000") {
          mainApp(req, res, next);
          console.log("main");
        } else if (req.hostname == "http://localhost:5000/login") {
          loginApp(req, res, next);
          console.log("login");
        }
      })
    .listen(port, () => console.log(`App listening on port: ${port}`));

I don’t understand how can I import the whole main.js module(the root route where the signup form exists, and the form data is also posted to the root route) and the login.js module(the ‘/login’ route where the login form exists.

How can I make index.js run both modules on the same port?

Advertisement

Answer

You have two options to achieve what you need:

  1. Using app.METHOD(PATH, HANDLER). Read more ExpressJs routing basic

    // index.js file

    const express = require("express");
    
    const app = express();
    const port = 5000;
    
    const mainApp = require("./main.js");
    const loginApp = require("./login.js");
    
    // route for /
    
    app.get("/", function(req,res, next) {
      mainApp(req, res, next);
      console.log("main");
    });
    
    //  route for /login
    app.get("/", function(req,res, next) {
      loginApp(req, res, next);
      console.log("login");
    });
    
    app.listen(port, () => console.log(`App listening on port: ${port}`));
    
  1. Using express.Router. This enables to break routing logic into modules. Use this option to handle routing in modules Main.js and login.js

    // main-router.js var express = require(‘express’) var router = express.Router() const mainApp = require(“./main.js”);

    // handler for /
    router.get('/', function (req, res) {
      mainApp(req, res, next);
      console.log("main");
    });
    
    module.exports = router
    

import module in index.js

const express = require("express");

const app = express();
const port = 5000;

const mainRouter = require("./main-router.js");
const loginApp = require("./login.js");

// route for /

app.get("/", mainRouter);

//  route for /login
app.get("/", function(req,res, next) {
  loginApp(req, res, next);
  console.log("login");
});

app.listen(port, () => console.log(`App listening on port: ${port}`));
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement