1

I'm trying to find a solution for my server and for its structure. What I want to do is separate.

app.post("/login", function(request, response) {  
});

app.post("/register", function(request, response) {  
});

app.get("/", function(request, response) {  
});

into different files. In my case, they are all standing in a file called "server.js". How can I separate them to work on them better?, and run all of them at the same time. I searched out this situation and I found different types of this case. But couldn't get a clear answer.

Ahmet Aziz Beşli
  • 1,280
  • 3
  • 19
  • 38
  • 1
    Possible duplicate of [How to include route handlers in multiple files in Express?](https://stackoverflow.com/questions/6059246/how-to-include-route-handlers-in-multiple-files-in-express) – jsalonen Aug 04 '17 at 17:41
  • Possible answer also in: https://stackoverflow.com/questions/33946972/how-to-split-node-js-files-in-several-files – jsalonen Aug 04 '17 at 17:41
  • I think it will be more beneficial to keep all of your routes in one file, but have those routes call functions in different files. That way you have all your routing in one place, but can separate the business logic functions. – Wolfie Aug 04 '17 at 17:42
  • @jsalonen those answers are a bit dated for modern express routing methodology. Correct, but dated. – Sterling Archer Aug 04 '17 at 17:46
  • @jsalonen thanks for the topics. They are all old but can be a solution for me. – Ahmet Aziz Beşli Aug 04 '17 at 17:53
  • @Matthew It's hard to work on it. I need to separate them. – Ahmet Aziz Beşli Aug 04 '17 at 17:53

2 Answers2

6

One thing you can do is incorporate a "router" into your express functions.

Here's a small sample of how I do it. (Shameless plug, feel free to view my games source code for a more full detailed router here)

server.js

import login from "./routes/login";
app.use("/login", login);

login.js

const router = express.Router();
router.route("/") //this points to the root route of /login
    .post((req, res) => {
        //post function here
    })
    .get((req, res) => {
        //get function here
    })

This allows you do do very specific functional routing, along with splitting the routers into different files (I store them in routes)

app_root
----routes
--------login.js
----server.js

This is a basic structure of how it will look in your file architecture. Very clean, very easy to modularize.

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • This is IMO the best solution. You can look at `server.js`, which is essentially the routing documented in one file, but you can also split up the functions/business logic into logical pieces. – Wolfie Aug 04 '17 at 17:55
1

As of the above comment by Sterling Archer, we use similar folder hierarchy for our development as well

server.js (node http server)
app/
  | app.js (express app)
  | api/
     | router.js (express master router to handler "/"
     | exampleApi/
          | exampleRouter.js (routes for example api)

PS: We use below yoeman generator to generate above folder structure for us yaoeman generator npmjs link , yo-dodan git repo (to get an idea of file structure)

Sahan Maldeniya
  • 1,028
  • 1
  • 14
  • 21