简体   繁体   中英

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.

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.

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

//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. 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

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

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. 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

Another way would be to export two handlers in your users.js file. Something like

index.js

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

And then in your users.js

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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