lundi 19 juin 2017

Setting correct structure for a node app

Introduction

So far I have three files, one test.js is a file where I have built three functions that work.

But now I am trying to structure using MVC or at least some pattern. So now I config.js and server.js to set out my routes and apply them to my app.

Question

Should I put my promise functions from test.js in my config.js or server.js or something else, Im just interested in how people would do this and whats the correct way of structuring NodeJS.

  1. server.js

In here start the server and apply the routes to my app

var configure = require('./config');
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;

// get an instance of router
var router = express.Router();
configure(router);

app.listen(port);
console.log('Server has started!! ' + port);

// apply the routes to our application
app.use('/', router);

  1. config.js

In here I build my routes

module.exports = function (router) {

    // route middleware that will happen on every request
    router.use(function (req, res, next) {

        // log each request to the console
        console.log(req.method, req.url);

        // continue doing what we were doing and go to the route
        next();
    });

// home page route (http://localhost:8080)
    router.get('/', function (req, res) {
        res.send('im the home page!');
    });

// sample route with a route the way we're used to seeing it
    router.get('/sample', function (req, res) {
        res.send('this is a sample!');
    });


// about page route (http://localhost:8080/about)
    router.get('/about', function (req, res) {
        res.send('im the about page!');
    });

// route middleware to validate :name
    router.param('name', function (req, res, next, name) {
        // do validation on name here
        console.log('doing name validations on ' + name);

        // once validation is done save the new item in the req
        req.name = name;
        // go to the next thing
        next();
    });

// route with parameters (http://localhost:8080/hello/:name)
    router.get('/hello/:name', function (req, res) {
        res.send('hello ' + req.params.name + '!');
    })

    // app.route('/login')

    // show the form (GET http://localhost:8080/login)
        .get('/login', function (req, res) {
            res.send('this is the login form');
        })

        // process the form (POST http://localhost:8080/login)
        .post('/login', function (req, res) {
            console.log('processing'); // shows on console when post is made
            res.send('processing the login form!'); // output on postman
        });
};

  1. test.js

In here is a list of functions that are a chain of promises getting data and API Keys

(small function, one of many that feed into each over)

var firstFunction = function () {
    return new Promise (function (resolve) {
        setTimeout(function () {
            app.post('/back-end/test', function (req, res) {
                console.log(req.body);
                var login = req.body.LoginEmail;
                res.send(login);
                resolve({
                    data_login_email: login
                });
            });
            console.error("First done");
        }, 2000);
    });
};

Aucun commentaire:

Enregistrer un commentaire