user10329774
user10329774

Reputation:

How can I split server.js in the right way?

I am writing my own app (both of back and frontend). I want to ask you guys if I am doing it in the right way. I want to split server.js to a few files (in PHP I would use include() for it) but I am not sure if it is the right way.

Here is some code example:

const app = require('express')(),
    fs = require('fs'),
    http = require('http').Server(app),
    io = require('socket.io')(https),
    path = require('path'),
    login_user = require('./routes/login_user'),
    add_user = require('./routes/add_user'),
    logout = require('./routes/logout');


app.post('/login_user', (req, res, next) => {
    login_user.go(req, res, next);
});

app.post('/add_user', (req, res) => {
    add_user.go(req, res);
});

app.get('/logout', (req, res) => {
    logout.go(req, res);
});

Please note that's not the whole code and I want to focus on spliting "routes" or "paths" to a few files. For example a whole API login system is in /routes/login_user.js file (exported).

Now I have got so many paths and code is looking a little bit weird.

app.post('/check_balance', (req, res) => {
    check_balance.go(req, res);
});

app.post('/add_incoming', (req, res) => {
    add_incoming.go(req, res);
});

app.post('/add_outgoing', (req, res) => {
    add_outgoing.go(req, res);
});

app.post('/add_category', (req, res) => {
    add_category.go(req, res);
});

app.post('/change_settings', (req, res) => {
    change_settings.go(req, res);
});

app.post('/login_user', (req, res, next) => {
    login_user.go(req, res, next);
});

app.post('/add_user', (req, res) => {
    add_user.go(req, res);
});

app.post('/verify_user', (req, res) => {
    verify_user.go(req, res);
});

app.get('/logout', (req, res) => {
    logout.go(req, res);
});

app.get('/check_settings', (req, res) => {
    check_settings.go(req, res);
});

app.get('/check_categories', (req, res) => {
    check_categories.go(req, res);
});

app.post('/remove_categories', (req, res) => {
    remove_categories.go(req, res);
});

app.get('/check_incomings', (req, res) => {
    check_incomings.go(req, res);
});

app.get('/check_outgoings', (req, res) => {
    check_outgoings.go(req, res);
});

app.get('/check_both', (req, res) => {
    check_both.go(req, res);
});

app.get('/check_outgoings_chart', (req, res) => {
    check_outgoings_chart.go(req, res);
});

app.get('/check_incomings_chart', (req, res) => {
    check_incomings_chart.go(req, res);
});

app.post('/remove_incomings', (req, res) => {
    remove_incomings.go(req, res);
});

app.post('/remove_outgoings', (req, res) => {
    remove_outgoings.go(req, res);
});

Upvotes: 0

Views: 782

Answers (3)

Malik Awan
Malik Awan

Reputation: 463

Make your server.js as simple as possible and extract all your routing logic to separate folder (possibly name it "routes"). If you also want to define yours schema, put it in separate folder ("models"). A complete solution can be like this:

in Model Folder: user.js

const mongoose = require("mongoose"); // In case if you want to use MongoDB
const userSchema = new mongoose.Schema({
  name: { type: String, required:true },
  email: { type: String, required: true },
  password: { type: String, required: true },

});
exports.User = User;

In routes folder: users.js

const { User } = require("../models/user");
const router = express.Router();

//define your routes here
router.get('/', async(req,res)=>{
  const users = await User.find();
res.send(users)
});
module.exports = router;

And finally in your server.js

const app = require('express')(),
    fs = require('fs'),
    http = require('http').Server(app),
    io = require('socket.io')(https),
    path = require('path'),
    users = require('./routes/users');

app.use("/api/users", users);  //With this API Endpoint you can access it like http://{your_domain}/api/users

If you want to make it more clean, you can wrap all routing paths to another folder. Lets call it "startup". with this you can do like this.

in your startup folder:

routes.js

const users = require("../routes/users");

module.exports = function(app) {
app.use("/api/users", users);
//add all routes here
}

Then in your server.js

require("./startup/routes")(app); //all routes will be imported

Upvotes: 1

Asad Ashraf
Asad Ashraf

Reputation: 1645

Put all the routes files in a folder with multiple files like User_routes.js can contain routes related to user and so on.

Also all you need to then is to export these routes from each file by using module.export.your_module and include them in your server file like in user routes :

// Login Page 
router.get('/login', (req, res) => res.render('login'));

// Register Page 
router.get('/register',(req, res) => {  
res.render('register'); });

then import it as

module.exports = router;

also include it as :

app.use('/users', require('./routes/users.js'));

app.use('/',(req,res,next)=>{
  console.log('I should handle it Now.');
res.render('404');
});

Upvotes: 0

cradapyx
cradapyx

Reputation: 283

I think this is what you need.

Let's say that there is a file called routers/login.js:

var express = require('express');
var router = express.Router();

router.get('/login', function(req, res) {
  // do something  
});

then app.js:

...
var login = require('./routes/login');
app.use("/login", login)

Upvotes: 0

Related Questions