简体   繁体   English

Express.js两条路径,例如在单个模块中进行注册和登录

[英]Express.js two routes like signup and signin in single module

I need help while putting different functions like sign up, sign in, delete a profile, edit profile, in a single file named Users in express. 在将不同功能(例如注册,登录,删除配置文件,编辑配置文件)放在一个名为Users in express的文件中时,我需要帮助。

I have put signup over the '/' and now I am unable to find the way to go to the sign in function in users file. 我已经在'/'上进行了注册,但是现在无法在users文件中找到进入登录功能的方法。

for signup, I used the express method as app.use('/signup' , Users) and I wanted to know that how can I access the sign in function now 对于注册,我将express方法用作app.use('/signup' , Users) ,我想知道如何立即访问登录功能

//this is the code in Users.js file
router.post("/", (req, res) => {
  var user = new User();
  user.name = req.body.name;
  user.DOB = req.body.DOB;
  user.email = req.body.email;
  user.city = req.body.city;
  user.password = req.body.password;
  user.gender = req.body.gender;
  user.image = req.body.image;
  user.Phone = req.body.PhoneNumber;
  user.MsgNumber = req.body.MsgNumber;
  user.about = req.body.about;
  user.JoinDate = new Date;
  user.save(function(err, result) {
    if (err) {
      res.json({
        status: err
      })
    } else {
      res.json({
        status: 'ok'
      });
    }
  });
});

//now the second function of signin
router.post("/signIn", passport.authenticate("local"), (req, res) => {
      if (usernotfound == 0) {
        res.send(JSON.stringify(req.user));
      } else {
        res.send('Not Found')
      }



// here is the code from the main server js file to send data to these functions

app.use('/signUp', users)
app.use('/signin', users)

{signup is on '/' so that is called directly as the root function. {signup在'/'上,因此直接称为根函数。 now how can i access the signin function} 现在如何访问登录功能}

Not quite sure what you are looking for but you are using the same handler for different routes when you do this: 不太确定您要查找什么,但是在执行此操作时,您对不同的路由使用了相同的处理程序:

app.use('/signUp', users)
app.use('/signin', users)

What you are telling express here is "I want the same thing to happen when users go to signUp and signin" 您在此处表示的意思是“我希望用户登录并登录时会发生同样的事情”

You can have the users route in same file but the handlers need to be different. 您可以让用户在同一文件中路由,但处理程序需要不同。

index.js index.js

const epxress = require('express')
const userRouter = require('./users')
const app = express()


app.use('/users, userRouter)

The index file is a simple express app which requires user.js 索引文件是一个简单的快速应用程序,需要user.js

users.js users.js

const expreess = require('express')
const router = express.Router()

router.post("/signIn", passport.authenticate("local"), (req, res) => {
   // Your code for signing in
})

router.post('/signUp', (req, res) => {
  // your code for signing up
})

module.exports = router    

So users.js is a simple express router. 所以users.js是一个简单的快速路由器。 So now visitors can go to /users/signIn and /users/signUp - basically they go to '/users' and get routed to the user.js file which has other routes defined /signIn and /signUp so the complete path becomes /users/signIn and /users/signUp 因此,现在访问者可以转到/ users / signIn和/ users / signUp-基本上,他们转到'/ users'并被路由到user.js文件,该文件具有定义了/ signIn和/ signUp的其他路由,因此完整的路径变为/ users / signIn和/ users / signUp

Another way would be to export two handlers in your users.js file. 另一种方法是在users.js文件中导出两个处理程序。 Something like 就像是

index.js index.js

app.post('/signIn', users.signIn)
app.post('/signUp', users.signUp)

And then in your users.js 然后在你的users.js中

exports.signIn = function(req, res) {
  // Your code for signin in
}

exports.signUp = function(req, res) {
  // Your code for signing up
}

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

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