-1

index.js

const AuthRouter = require("./Routes/Auth/signup")

app.use("/account", AuthRouter)

signup.js

router.post("/", async (req, res) => {
          res.send("Signup")
})

This Code works...

But I won't like this, It's Possible in Express.js

index.js

const AuthRouter = require("./Routes/Auth/urls")

app.use("/account", AuthRouter)

urls.js

app.use("/signup", signup)
app.use("/login", login)

signup.js

router.post("/", async (req, res) => {
          res.send("Signup")
})

login.js

router.post("/", async (req, res) => {
          res.send("Login")
})
Mr.k1n1
  • 74
  • 1
  • 11
  • your examples are not omplete, and please, just show us what is the relevant files that the errors occurs, and include the import and creaton of the variables you use.. – adir abargil Dec 27 '20 at 09:10

2 Answers2

1

You could do it inline as well. I prefer it that way. like this on the server:

app.use('/users', require('../utils/api/user'))

And the route file like this called user.js in the given directory:

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

router.post('/login', (req, res) => {
    res.render('user', {title: 'User', user})
})
router.post('/signup', (req, res) => {
  res.render('user', {title: 'User', user})
}

module.exports = router

Now the route on client side to login is /users/login and to signup is /users/signup

Manaweb
  • 56
  • 4
0

Yes, you can do that. For instance, you will have to dedicate a specific file for your routes. Say routes.js like this:

import express from "express";
import { Signup } from "./";

const router = express.Router();

router.post("/signup", signup);

export { router }

Also, you have to have something like this in your server.js file:

import { router as usersRoutes } from "./api/routes/routes";
app.use("/api", usersRoutes);

Now, you can send HTTP POST requests to localhost:3000/api/signup