Best way to use OOP in Express REST API?

I am going to execute a project using only node. It was a lot of fun, but sometimes I get a little lost in it, and I want to try to get an understanding, because I'm confused, so I will build it correctly and not too overloaded. Anyway, here is the problem:

I have a REST API that uses Express and mysql. I configured mysql:

app.js

//Variables, move these to env
var dbOptions = {
    host: config.db_config.host,
    user: config.db_config.user,
    password: config.db_config.password,
    port: config.db_config.port,
    database: config.db_config.database
};
app.use(myConnection(mysql, dbOptions, 'single'));

and then I include my routes, passing the routes and the log middleware so that I can use them in the route:

app.js cont.

var userRoute = require('./routes/users.js')(app,log);
app.use('/users', userRoute);

It was actually a bit confusing, but now I get it, I have to pass them to the module so that the module can receive the data.

, , , , , , , , ? . .

/users.js

var User = require('../controllers/User.js');

module.exports = (function(app,log) {
var userR = express.Router();

userR.post('/register', function(req,res){
        var email = req.body.email;
        var password = req.body.password;
        var firstName = req.body.first_name;
        var lastName = req.body.last_name;
        var userId;

        try {
            var query;
            var status = 200;
            var response = '';

            var newUser = {
                email: email,
                password:password,
                first_name: firstName,
                last_name: lastName,
                password: password
            };

            var user = new User(req,res);

            user.register(newUser);
...

};

/user.js

module.exports = function User(req,res) {
this.id = 0;
    this.register = function(newUser){

        var _this = this;
        var deferred = q.defer();
req.getConnection(function(err,connection){
...

, . - req.getConnection ..

.

+4
1

, .

, . , , , , . , , , . , Express 4x.

/users.js

var User = require('../controllers/user.js');
var userRouter = express.Router();
userRouter.post("/register", User.register);
module.exports = userRouter;

, , /.

, ( app.js) :

app.js

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

, (, , ..).

Express userRouter "/users". "/users/register".

, , , -. , :

/user.js

var User = require("../models/user.js")
var register = function(req, res, next){
    var email = req.body.email;
    var password = req.body.password;
    var firstName = req.body.first_name;
    var lastName = req.body.last_name;
    var userId;
    var params = {
            email: email,
            password:password,
            first_name: firstName,
            last_name: lastName,
            password: password
        };
    var newUser = new User(params);

    try {
       newUser.register();
        // do other things...
    }
};
module.exports = {register: register};

, , , UserModel. , . , , (, + pw, FB, ). (user.register ), , !

, UserModel:

/models/user.js

var connection = require("../lib/connection.js");
var User = function(params){
   this.email = params.email;
   // ...etc
};

User.prototype.register = function(newUser){
    connection.getConnection(function(error, connection){
        //connection.doWhatever();
    });
};

module.exports = User;

, , . , . , , .

/lib/connection.js

/*
This will be in some JSON config we'll say
var dbOptions = {
    host: config.db_config.host,
    user: config.db_config.user,
    password: config.db_config.password,
    port: config.db_config.port,
    database: config.db_config.database
};

*/
//This will depend on which version/module/db you're using, but here what mine looks like
var MySQL = require("mysql");
var config = require("../config/db.json");
connectionPool = MySQL.createPool({host: config.db_config.host, ...});

var getConnection = function(done){
   connectionPool.getConnection(done);
};

module.exports = {getConnection: getConnection};

, , , , , , , . , , :).

, CoffeeScript, , , . , .

Cheers,

+10

All Articles