Skip to content

Instantly share code, notes, and snippets.

@alexfedoseev
Last active Aug 29, 2015
Embed
What would you like to do?
Express server: iteration 1
/* server.app.js */
// This is entry point of our `app` bundle.
// And here we collect bundle specific stuff
// to pass it to reusable abstraction part.
// We can add `server.admin.js` bundle etc.
// We gonna use ES6 / ES7 syntax in our modules
// So we need to transform it to ES5 with every `require` from here on
require('babel/register')({
extensions: ['.js', '.jsx'],
stage : 0
});
// Middleware function of `app` bundle
var initter = require('./app/bundles/app/initters/server');
// Bundle settings
var config = require('./config/server.app');
// Starting express server
require('./server')(initter, config);
/* server.js */
// This is shared between all bundles
import express from 'express';
import parser from 'body-parser';
import cookies from 'cookie-parser';
import path from 'path';
export default (initter, config) => {
// Defining some globals
global.__CLIENT__ = false;
global.__SERVER__ = true;
global.__DEV__ = config.env !== 'production';
// Initializing app
const app = express();
// Parsing json bodies
app.use(parser.json());
// Parsing urlencoded bodies
app.use(parser.urlencoded({ extended: true }));
// Parsing cookies
app.use(cookies());
// Serving static files from `public` dir
app.use(express.static(path.join(__dirname, 'public')));
// Here we are!
// Transferring incoming requests to middleware function of the bundle
app.use('/', initter);
// Setting up port to listen
app.set('port', config.appPort);
// And listening it
app.listen(app.get('port'), function() {
console.log(`=> 🚀 Express ${config.bundle} ${config.env} server is running on port ${this.address().port}`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment