0

I have a folder for my routes, so I want to export the routes to my app.js using axios.

I just don't know how to add this axios routes to my app.js file as I do with normal routers from express.Router()

This is my USER.JS file inside routes folder on my porject:

const express = require('express')
const router = express.Router()
const userController = require('../controllers/userController')
const axios = require('axios')

router.get('/user', userController.getUserLogin)
router.get('/userRegister', userController.getUserRegister)
router.post('/user', userController.postUserLogin)
router.post('/userRegister', userController.postUserRegister)

module.exports = axios.get('/user', userController.getUserLogin)

module.exports = router

This is my app.js:

const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const userRoutes = require('./routes/user')

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }))
app.set('view engine', 'ejs')

app.use('/', userRoutes)

app.listen(process.env.PORT || 5000, () => {
    console.log(`application running`)
})
Selaron
  • 6,105
  • 4
  • 31
  • 39

1 Answers1

0

how to add this axios routes

Simple answer: There's no axios routes

From npm axios describes itself as:

Promise based HTTP client for the browser and node.js

Notice the word client. It's a client not server. Its function is to make http calls to some resource served by server.

From npm, the top 2 listed features

Make XMLHttpRequests from the browser

Make http requests from node.js

To make express routes you either use instance of default express class or express.Router() class.

const express = require('express')
const app = express();

// route
app.get('/myroute', (req, res) => {})

// using Router
const router = express.Router()
router.get('/myroute', (req, res) => {})
Community
  • 1
  • 1
1565986223
  • 6,420
  • 2
  • 20
  • 33