Geeks Trick

Build Node Express App Structure

 

The app folder

Download & install Node Server & Git-version controll.

let’s start with building the app. As for Node Express App first, download the required things. Create a folder contains the node express app structured files.
Open up the git version control (terminal), then go to your current directory of your app and install node packages through command.

$ npm install


The Main App File

The core file from where the app starts its execution. This files connect the whole folder structure i.e model, controller, middleware etc.

app.js
var express = require('express'); 
var app = express();
var bodyParser = require('body-parser'); 
var session = require('express-session');
var fs = require('fs');
var morgan = require('morgan');
var db = require('./models/db.js');
var router = express.Router(); 

app.use(bodyParser.json());  
app.use(bodyParser.urlencoded({
  extended: true
}));

if(GLOBAL.SQLpool === undefined){
	GLOBAL.SQLpool = db.createPool(); //create a global sql pool connection
}

app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true
}));

morgan.token('res', function getId(res) {
	return res;
});

var accessLogStream = fs.createWriteStream(__dirname + '/logs/access.log', {flags: 'a'});

app.use(morgan(':req[body] :res[body]', {stream: accessLogStream}));

app.use(require('./controllers'));

app.use('/', express.static(__dirname + '/client'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));

app.listen('3000', function(){
	console.log("Connected on port 3000.");
});

The app.js file contain the node express models package (inbuild packages).

The Gulp File

Install Gulp Globally

$ npm install gulp -g

The gulp file make the app to run on localhost on port localhost:3000.
To start the server go to the file directory in terminal and just enter gulp

$ gulp 
gulpfile.js
var gulp   = require( 'gulp' ),
    server = require( 'gulp-develop-server' )
	jshint = require('gulp-jshint');
	
gulp.task('lint', function() {
  return gulp.src('app.js')
    .pipe(jshint())
    .pipe(jshint.reporter('default'));
});
	
	// run server 
gulp.task( 'server:start', function() {
    server.listen( { path: './app.js' } );
});
 
// restart server if app.js changed 
gulp.task( 'server:restart', function() {
    gulp.watch( [ './app.js', './controllers/**/*', './models/**/*', './middlewares/**/*' ], server.restart );
});

gulp.task('default', ['lint','server:start','server:restart']);

The models Folder

The Models Folder contains the database connection code in db.js
And also contain th querie related to the application in module wise. Each module of the application is seperated whith there query.

 

The Controller Folder

The Controller folder contains the API for the application such as products users.

 

The Middleware Folder

The Middleware folder contains the authentication part for the application.
example Permission for the following users etc.

Demo : Folder Structure
Node Express App Structure

Rehmaanali

Hi This Is Rehmaanali.
Blogging Is Just A Hobby.
Graduate In Bsc Computer Science

Add comment

Author

Rehmaanali

Hi This Is Rehmaanali.
Blogging Is Just A Hobby.
Graduate In Bsc Computer Science

Subscribe To Our Newsletter

Join our mailing list to receive the latest Articles news and updates from our team. 

You have Successfully Subscribed!