简体   繁体   English

使用快速服务器制作注册表单和登录表单

[英]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.所以我基本上是在我的本地主机上创建一个简单的网站,其中包含一个注册表单和一些其他 html 元素。 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.因此,当用户填写表单并将其提交到根路由(“/”)时,email 会发送给订阅者,其中包含使用 nodemailer 的随机密码,然后该密码将用于登录用户仪表板。 The whole data is submitted as a mongodb document which I had setup.整个数据作为我设置的 mongodb 文档提交。 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.至此,过程顺利进行,当用户注册时,邮件被发送到订阅者的 email id,然后他们被重定向到登录页面。 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.我不明白如何导入整个main.js模块(注册表单存在的根路由,并且表单数据也发布到根路由)和login.js模块('/login' 路由登录表单所在的位置。

How can I make index.js run both modules on the same port?如何让index.js在同一个端口上运行两个模块?

You have two options to achieve what you need:您有两种选择来实现您的需求:

  1. Using app.METHOD(PATH, HANDLER) .使用app.METHOD(PATH, HANDLER) Read more ExpressJs routing basic阅读更多ExpressJs 路由基础

    // index.js file // index.js 文件

    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 .使用express.Router This enables to break routing logic into modules.这可以将路由逻辑分解为模块。 Use this option to handle routing in modules Main.js and login.js使用此选项处理模块Main.jslogin.js中的路由

    // main-router.js var express = require('express') var router = express.Router() const mainApp = require("./main.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.jsindex.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}`));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM