R
R
Rag'n' Code Man2020-11-04 15:28:30
Node.js
Rag'n' Code Man, 2020-11-04 15:28:30

How to split a nodejs application into components?

Good afternoon, I'm writing a server for a NodeJS game and I ran into such a problem: I want to split my application into components, such as: registration, authorization, etc.

I started like this:

index.js

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

app.listen(3000, () => { console.log('server successfully started') })

module.exports = app


Then in the components/auth/auth.js file I wrote the following code:

components/auth/auth.js
const app = require('../../index.js')

app.post('/auth', (req, res) => {
    res.end('request has been handled')
})


Then I ran the whole thing, made a postman request to localhost:3000/auth and got a 404 error.

If I move this code into an index.js file , then everything will work.

Question: how to distribute a NodeJS application into components?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-11-04
@iDmitriyWinX

https://expressjs.com/ru/guide/routing.html#expres...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question