The structure of the project files?

Recently started learning Node, framework Express... asked the question on the structure of the project files, did a build, need your opinion.. or is there a ready solution?

backend
. . . controllers
. . . . . . posts.controller.js
. . . models
. . . . . . posts.models.js
. . . db.js
. . . router.js
. . . server.js


posts.models.js
const mongoose = require('mongoose');

const postsSchema = mongoose.Schema({
 title: String,
 description: String,
 date: {
 type: Date,
 default: Date.now()
}
});

module.exports = mongoose.model('posts', postsSchema);


posts.controller.js
const Posts = require('../models/posts.models');

exports.module = {
 async post (req, res) {
 const post = new Posts({
 title: req.body.title
 description: req.body.description
});

 await post.save();

Res. json({
 message: 'created',
 data: post
})
},

 all async (req, res) {
 const posts = await Posts.find()
Res. json({
 data: posts
})
}
};


router.js
const load = (controller) => require(`./controllers/${controller}.js`);

module.exports = function (app) {
 app.get('/', load('posts.controller').module.all);
 app.post('/posts', load('posts.controller').module.post);
};


server.js
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const db = require('./db');

const app = express();

app.use(cors());
app.use(bodyParser.json());

require('./router')(app);


db.connect('mongodb://localhost:27017/blog', (err) => {
 if (err) return console.log(err);
 app.listen('3000', () => console.log('Server started'))
});
March 19th 20 at 09:25
0 answer

Find more questions by tags Express.jsMongooseNode.js